android – 如何将Onclick监听器添加到Recycler视图

我正在开发一个简单的记事本应用程序,我首先使用listView来显示所有注释.但现在我使用的是RecyclerView.当我使用listview时,我使用OnItemClickListener将数据传递给另一个活动来编辑注释
现在我很困惑在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();

     }

现在我该如何处理回收查看请帮忙

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).使用适配器中的接口
https://antonioleiva.com/recyclerview-listener/

我建议使用界面的第二种方式

如何使用recycleritemclick的界面

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分配给界面

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

在MainActivity.java中
使用适配器绑定项目单击

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
}

希望这有助于你,如果你需要任何帮助,你可以问

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...