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

前端之家收集整理的这篇文章主要介绍了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
}

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

猜你在找的Android相关文章