Recyclerview 中 item监听position错误问题

发布于 2022-09-12 00:39:52 字数 5148 浏览 18 评论 0

最近学习了Android,在写一个小项目练习,其中用到了Recyclerview+Cardview实现消息列表。但是当实现每个Cardview的监听的时候发现点击的位置和监听到的位置不符,请问是什么原因呢?
res.gif

下面是我的代码(去掉了不相关的和服务器交互的部分)
PostCardViewAdapter

public class PostCardViewAdapter extends RecyclerView.Adapter<com.coursedesign.weibo.tools.PostCardViewAdapter.ItemCardViewHolder>{  
    private List<PostBean> beans;  
    private LayoutInflater mInflater;  
    private Context mContext;  
    private ItemCardViewHolder holder;  

    private PostBean curr_post;  

    public PostCardViewAdapter(Context context, ArrayList<PostBean> dataList){  
        this.mContext = context;  
        beans = dataList;  
        mInflater= LayoutInflater.from(context);  
    }  
  
    @Override  
    public ItemCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
        View view = mInflater.inflate(R.layout.single_post_layout,parent,false);  
        return new ItemCardViewHolder(view);
    }  
  
    @Override  
    public void onBindViewHolder(com.coursedesign.weibo.tools.PostCardViewAdapter.ItemCardViewHolder viewHolder, int position) {  
        holder = viewHolder;  
        if (mOnItemClickListener != null) {  
            holder.itemView.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    int pos = holder.getLayoutPosition();  
                    mOnItemClickListener.onItemClick(holder.itemView, pos);  
                }  
            });  
        }  
    }  
  
    @Override  
  public int getItemCount() {  
        return beans.size();  
  }  
  
    public static class ItemCardViewHolder extends RecyclerView.ViewHolder{  
        private CardView item_cardview;  
        private TextView author;  
        private TextView date;  
        private TextView content\_text;  
        private TextView like;  
        private TextView comment;  
        private TextView repost;  
        private CircularImageView author_img;  
  
        public ItemCardViewHolder(View itemView) {  
            super(itemView);  
            author_img = (CircularImageView)itemView.findViewById(R.id.author_image);  
            item_cardview=(CardView)itemView.findViewById(R.id.post_cardview);  
            author = (TextView)itemView.findViewById(R.id.author_name);  
            date = (TextView)itemView.findViewById(R.id.post_time);  
            content_text = (TextView)itemView.findViewById(R.id.content_text);  
            like = (TextView)itemView.findViewById(R.id.like_count_text);  
            comment = (TextView)itemView.findViewById(R.id.comment_count_text);  
            repost = (TextView)itemView.findViewById(R.id.repost_count_text);  
           }  
  
    }  
  
  
    public interface OnItemClickListener {  
        void onItemClick(View view, int position);  
  
        void onItemLongClick(View view, int position);  
  }  
  
    private OnItemClickListener mOnItemClickListener;  
  
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {  
        this.mOnItemClickListener = mOnItemClickListener;  
    }  
}

Fragment

public class PostListFragment extends Fragment {  
    private View view; //定义view用来设置fragment的layout  
    public RecyclerView recycler_cardview;//定义RecyclerView  
    private ArrayList<PostBean> postList = new ArrayList<PostBean>();  
    //自定义recyclerveiw的适配器  
    private PostCardViewAdapter recyclerAdapter;  
  
    @Nullable  
    @Override  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        //获取fragment的layout  
        view = inflater.inflate(R.layout.fragment_post_list, container, false);  
        //对recycleview进行配置  
        initRecyclerView();  
  
        return view;  
  }  
  
    private void initRecyclerView() {  
        //获取RecyclerView  
        recycler_cardview = (RecyclerView)view.findViewById(R.id.post_list_view);  
        //设置布局  
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());  
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);  
        recycler_cardview.setLayoutManager(linearLayoutManager);  
        //创建adapter  
        recyclerAdapter = new PostCardViewAdapter(getActivity(), postList);  
        recyclerAdapter.setOnItemClickListener(new PostCardViewAdapter.OnItemClickListener() {  
            @Override  
            public void onItemClick(View view, int position) {  
                Toast.makeText(getContext(),"click: " + position, Toast.LENGTH_SHORT).show();  
            }  
  
            @Override  
            public void onItemLongClick(View view, int position) {  
                Toast.makeText(getContext(),"long click: " + position, Toast.LENGTH_SHORT).show();  
            }  
        });  
        //给RecyclerView设置adapter  
        recycler_cardview.setAdapter(recyclerAdapter);  
  
    }  
  
}

求大神解答,谢谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2022-09-19 00:39:52

建议是在onCreateViewHolder的地方注册监听,像下面这样:

@NonNull  
@Override  
public V onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {  
    final V holder = getViewHolder(parent, viewType);  
    if (mClickListener != null) {  
        holder.itemView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mClickListener.onItemClick(holder.itemView, getItem(holder.getLayoutPosition()), holder.getLayoutPosition()); 
            }  
        });  
  }  
    if (mLongClickListener != null) {  
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {  
            @Override  
            public boolean onLongClick(View v) {  
                mLongClickListener.onItemLongClick(holder.itemView, getItem(holder.getLayoutPosition()), holder.getLayoutPosition());                 
                return true;  
            }  
        });  
    }  
    return holder;  
}

源码的链接可以发你参考:https://github.com/xuexiangjy...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文