Java – how to use onclicklistener in the recycle bin view?
•
Java
See English answer > recyclerview onclick34
package com.albumlist.albumlist; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private AlbumData[] itemsData; public MyAdapter(AlbumData[] itemsData){ this.itemsData = itemsData; } public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private TextView txtViewTitle; private ImageView imgViewIcon; public ViewHolder(View itemLayoutView) { super(itemLayoutView); itemLayoutView.setOnClickListener(this); txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.album_title); imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.album_icon); } @Override public void onClick(View v) { } } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { View itemLayoutView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.data_layout,null); ViewHolder viewHolder = new ViewHolder(itemLayoutView); return viewHolder; } @Override public void onBindViewHolder(ViewHolder viewHolder,int position) { viewHolder.txtViewTitle.setText(itemsData[position].getTitle()); viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl()); } @Override public int getItemCount() { return itemsData.length; } }
Solution
This concept is well summarized in Xaver Kapeller's comments If you are looking for a simple way to manage the recyclerview interaction, just like the traditional interaction of listview, please check the bignerdranch's recyclerview multiselect library on GitHub They have a sample application that can be explored, which implements a series of onclicklisteners and onlongclicklisteners with multiple selection
The following is the code snippet of how bignerdranch implements the listener in the fragment through the adapter and viewholder. In this case, it is actually an extension of the swapingholder of the library itself
public CrimeHolder(View itemView) { super(itemView,mMultiSelector); mTitleTextView = (TextView) itemView.findViewById(R.id.crime_list_item_titleTextView); mDateTextView = (TextView) itemView.findViewById(R.id.crime_list_item_dateTextView); mSolvedCheck@R_925_2419@ = (Check@R_925_2419@) itemView.findViewById(R.id.crime_list_item_solvedCheck@R_925_2419@); itemView.setOnClickListener(this); itemView.setLongClickable(true); itemView.setOnLongClickListener(this); } public void bindCrime(Crime crime) { mCrime = crime; mTitleTextView.setText(crime.getTitle()); mDateTextView.setText(crime.getDate().toString()); mSolvedCheck@R_925_2419@.setChecked(crime.isSolved()); } @Override public void onClick(View v) { if (mCrime == null) { return; } if (!mMultiSelector.tapSelection(this)) { selectCrime(mCrime); } } @Override public boolean onLongClick(View v) { ((AppCompatActivity) getActivity()).startSupportActionMode(mDeleteMode); mMultiSelector.setSelected(this,true); return true; } } private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> { @Override public CrimeHolder onCreateViewHolder(ViewGroup parent,int pos) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item_crime,parent,false); return new CrimeHolder(view); } @Override public void onBindViewHolder(CrimeHolder holder,int pos) { Crime crime = mCrimes.get(pos); holder.bindCrime(crime); Log.d(TAG,"binding crime" + crime + "at position" + pos); } @Override public int getItemCount() { return mCrimes.size(); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码