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

android – 如何将Onclick监听器添加到Recycler视图,第1张

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

}

@OverrIDepublic 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;}@OverrIDepublic 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());}@OverrIDepublic 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 – 如何将Onclick监听器添加到Recycler视图全部内容,希望文章能够帮你解决android – 如何将Onclick监听器添加到Recycler视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134461.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存