Recyclerview中item监听位置错误

ZZZPHOENIX
  • 33

最近学习了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(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 PostCardViewAdapter.OnItemClickListener mOnItemClickListener;  
  
    public void setOnItemClickListener(PostCardViewAdapter.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);  

    }  
  
}

求大神解答,谢谢!

回复
阅读 1.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏