您好,登錄后才能下訂單哦!
Adapter 中如何為RecyclerView添加單擊事件,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
RecyclerView 的每個Item的點擊事件并沒有像 ListView 一樣封裝在組件中,需要 Item 的單擊事件時就需要自己去實現,在 Adapter 中為RecyclerView 添加單擊事件參考如下:
在 RecyclerView 的 Adapyer 中定義單擊事件的回調接口:
/** * 定義 RecyclerView 選項單擊事件的回調接口 */ public interface OnItemClickListener{ //參數(父組件,當前單擊的View,單擊的View的位置,數據) void onItemClick(RecyclerView parent,View view, int position, String data); }
在RecyclerView的Adapyer中聲明該接口,并提供setter方法:
private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; }
在RecyclerView的Adapyer類實現View.OnClickListener接口,并重寫onClick(View view)方法,然后設置給接口的事件監聽:
public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{ ... view.setOnClickListener(this);//設置監聽器 ... @Override public void onClick(View view) { //根據RecyclerView獲得當前View的位置 int position = recyclerView.getChildAdapterPosition(view); //程序執行到此,會去執行具體實現的onItemClick()方法 if (onItemClickListener!=null){ onItemClickListener.onItemClick(recyclerView,view,position,mList.get(position)); } } ... }
在MainActivity中通過Adapter設置每個Item的單擊事件:
adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() { @Override public void onItemClick(RecyclerView parent, View view, int position, String data) { Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show(); } });
/** * Created by jzman on 2017/5/13 0013. * RecycleView的Adapter */ public class RvAdapter1 extends RecyclerView.Adapter<RvAdapter1.DataViewHolder> implements View.OnClickListener{ private Context mContext; private RecyclerView recyclerView; private ArrayList<String> mList; public RvAdapter1() {} public RvAdapter1(Context mContext, ArrayList<String> mList) { this.mContext = mContext; this.mList = mList; } /** * 用于創建ViewHolder * @param parent * @param viewType * @return */ @Override public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item,null); view.setOnClickListener(this); //使用代碼設置寬高(xml布局設置無效時) view.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); DataViewHolder holder = new DataViewHolder(view); return holder; } /** * 綁定數據 * @param holder * @param position */ @Override public void onBindViewHolder(DataViewHolder holder, int position) { holder.tv_data.setText(mList.get(position)); } /** * 選項總數 * @return */ @Override public int getItemCount() { return mList.size(); } @Override public void onClick(View view) { //根據RecyclerView獲得當前View的位置 int position = recyclerView.getChildAdapterPosition(view); //程序執行到此,會去執行具體實現的onItemClick()方法 if (onItemClickListener!=null){ onItemClickListener.onItemClick(recyclerView,view,position,mList.get(position)); } } /** * 創建ViewHolder */ public static class DataViewHolder extends RecyclerView.ViewHolder{ TextView tv_data; public DataViewHolder(View itemView) { super(itemView); tv_data = (TextView) itemView.findViewById(R.id.tv_recycle); } } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } /** * 定義RecyclerView選項單擊事件的回調接口 */ public interface OnItemClickListener{ //參數(父組件,當前單擊的View,單擊的View的位置,數據) void onItemClick(RecyclerView parent,View view, int position, String data); } /** * 將RecycleView附加到Adapter上 */ @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); this.recyclerView= recyclerView; } /** * 將RecycleView從Adapter解除 */ @Override public void onDetachedFromRecyclerView(RecyclerView recyclerView) { super.onDetachedFromRecyclerView(recyclerView); this.recyclerView = null; } }
/** * Created by jzman on 2017/5/13 0013. */ public class MainActivity extends AppCompatActivity { private RecyclerView rv; RvAdapter1 adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.rv); //設置布局管理器 // rv.setLayoutManager(new LinearLayoutManager(this));//線性 rv.setLayoutManager(new GridLayoutManager(this,4));//線性 // rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//線性 adapter = new RvAdapter1(this,initData()); adapter.setOnItemClickListener(new RvAdapter1.OnItemClickListener() { @Override public void onItemClick(RecyclerView parent, View view, int position, String data) { Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show(); } }); rv.setAdapter(adapter); } public static ArrayList<String> initData(){ ArrayList<String> arrayList = new ArrayList<>(); for (int i=0;i<50;i++){ arrayList.add("第"+i+"條數據"); } return arrayList; } }
看完上述內容,你們掌握Adapter 中如何為RecyclerView添加單擊事件的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。