Android开发RecyclerView中Holder.absoluteAdapterPosition返回-1的解决办法

Android开发RecyclerView中Holder.absoluteAdapterPosition返回-1的解决办法

背景

最近在学习安卓开发,参考书是《第一行代码——Android(第三版)》,第四章UI开发时碰到了如下问题。

问题描述

在RecyclerView中,为了响应点击事件,我们通常在Adapter适配器中重写onCreateViewHolder()等函数,并为每个控件写一个setOnClickListener,但是我在onCreateViewHolder()中加监听器时,要获取的position总是返回-1,导致抛出越界异常。

解决办法

将所有的监听器代码放到onBindViewHolder方法中,就可以返回正确的position了。

修改后的代码:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val fruit = fruitList[position]
    holder.fruitImage.setImageResource(fruit.imageId)
    holder.fruitName.text = fruit.name

    val viewHolder = holder//ViewHolder(view)
    viewHolder.itemView.setOnClickListener{
        val position = viewHolder.absoluteAdapterPosition
        Log.e("itemViewMaeesage","fruitList.length=${fruitList.size},absoluteAdapterPosition=$position.")
        val fruit = fruitList[position]
        Toast.makeText(context, "You clicked the view ${fruit.name}", Toast.LENGTH_SHORT).show()
    }
    viewHolder.fruitImage.setOnClickListener{
        val position = viewHolder.absoluteAdapterPosition
        Log.e("fruitImageMaeesage","fruitList.length=${fruitList.size},absoluteAdapterPosition=$position.")
        val fruit = fruitList[position]
        Toast.makeText(context, "You clicked the image ${fruit.name}", Toast.LENGTH_SHORT).show()
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
To implement a custom Adapter in RecyclerView, you need to follow these steps: 1. Create a new class that extends RecyclerView.Adapter. This class will act as the adapter for your RecyclerView. 2. Override the onCreateViewHolder method, which is responsible for creating the ViewHolder objects for each item in the RecyclerView. In this method, you need to inflate the layout for the item and create a new ViewHolder object that holds a reference to the views in the layout. 3. Override the onBindViewHolder method, which is responsible for binding the data to the views in the ViewHolder object. In this method, you need to get the data for the current item and set it to the views in the ViewHolder object. 4. Override the getItemCount method, which returns the total number of items in the RecyclerView. Here's an example code snippet for implementing a custom Adapter in RecyclerView: ``` public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> mData; public MyAdapter(List<String> data) { mData = data; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_item_layout, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { String item = mData.get(position); holder.mTextView.setText(item); } @Override public int getItemCount() { return mData.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.text_view); } } } ``` In this example, the MyAdapter class extends RecyclerView.Adapter and takes a list of strings as data in its constructor. The onCreateViewHolder method inflates the layout for each item from a layout resource file and creates a new ViewHolder object. The onBindViewHolder method sets the text for each item in the RecyclerView. Finally, the getItemCount method returns the total number of items in the RecyclerView. Note that in this example, the ViewHolder class is defined as a static inner class of the MyAdapter class. This is a common practice to keep the code organized and to avoid memory leaks.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值