gpt4 book ai didi

android - 如何将 Onclick 监听器添加到回收站 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:21 24 4
gpt4 key购买 nike

我正在开发一个简单的记事本应用程序,首先我使用 listView 来显示所有笔记。但现在我正在使用 RecyclerView。当我使用 ListView 时,我使用 OnItemClickListener 将数据传递给另一个 Activity 以编辑注释现在我很困惑在 Recylerview 中该做什么

对于 listView 我正在使用这个

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = filenames.get(position).getName();
note = filenames.get(position).getShorttext();
Alert(); // this method is in main activity

}
});*/


public void Alert()
{
final AlertDialog dialog;
View mview = getLayoutInflater().inflate(R.layout.dialog_pass,null);
final EditText mEdittext = (EditText) mview.findViewById(R.id.Epass);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(mview);
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String col = mEdittext.getText().toString();
String password = dBhelper.searchpass(col);
if (col.equals(password)) {
Intent intent = new Intent(MainActivity.this,Note2.class);
intent.putExtra("Name",name);
intent.putExtra("Note",note);
startActivity(intent);
} else {
Toast temp = Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT);
temp.show();
}

}
});
mBuilder.setNegativeButton("Cancel",null);
mBuilder.setCancelable(false);
dialog = mBuilder.create();
dialog.show();

}

现在我应该为回收 View 做什么请帮忙

  public class RecycleViewAdapter extends 
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;

public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;

@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());

ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;

}

@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
mainHolder.title.setText(model.getName());
mainHolder.note.setText(model.getShorttext());

}

@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}

最佳答案

你可以用两种方式处理这个问题

1).手势触控 https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/

2).在适配器中使用接口(interface) https://antonioleiva.com/recyclerview-listener/

我建议使用接口(interface)的第二种方式

recycleritemclick接口(interface)的使用方法

public class RecycleViewAdapter extends 
RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
private List<FileName> fileNames;
private Context context;

//declare interface
private OnItemClicked onClick;

//make interface like this
public interface OnItemClicked {
void onItemClick(int position);
}

public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}

现在将点击分配给界面

@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
//............//
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}

在适配器类的末尾,从完成括号开始,创建一个将 itemclick 分配给接口(interface)的方法

public void setOnClick(OnItemClicked onClick){
this.onClick=onClick;
}

在MainActivity.java中将item click与adapter绑定(bind)

public class MainActivity extends Activity implements OnItemClicked {

private RecyclerView mRecyclerView;
private CityAdapter mAdapter;
private List<City> cities;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);

mRecyclerView = (RecyclerView)findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mAdapter = new CityAdapter(cities, R.layout.row_city, this);
mRecyclerView.setAdapter(mAdapter);

mAdapter.setOnClick(MainActivity.this); // Bind the listener
}

@Override
public void onItemClick(int position) {
// The onClick implementation of the RecyclerView item click
//ur intent code here
}
}

Kotlin 代码请引用 How to handle recyclerview item click in kotlin?

更多查询?评论。

关于android - 如何将 Onclick 监听器添加到回收站 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44151979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com