using notifyItemRemoved or notifyDataSetChanged with RecyclerView in Android

后端 未结 8 2249
忘掉有多难
忘掉有多难 2020-12-04 17:28

I am creating a list of cards to display using the RecyclerView, where each card has a button to remove that card from the list.

When i use notifyItemRemoved

8条回答
  •  遥遥无期
    2020-12-04 17:54

    **my solution looks like this**
    
    this way is unnecessary to use the heavy method:
     //notifyItemRangeChanged(xx,xx)
    
    /**
     * 
     * recyclerView的item中的某一个view,获取其最外层的viewParent,也就是item对应的layout在adapter中的position
     *
     * @param recyclerView
     * @param view:can be the deep one inside the item,or the item itself .
     * @return
     */
    public static int getParentAdapterPosition(RecyclerView recyclerView, View view, int parentId) {
        if (view.getId() == parentId)
            return recyclerView.getChildAdapterPosition(view);
        View viewGroup = (View) view.getParent();
        if (viewGroup != null && viewGroup.getId() == parentId) {
            return recyclerView.getChildAdapterPosition(viewGroup);
        }
        //recursion
        return getParentAdapterPosition(recyclerView, viewGroup, parentId);
    }
    
    
    
    
    //wherever you set the clickListener .
    holder.setOnClickListener(R.id.rLayout_device_item, deviceItemClickListener);
    holder.setOnLongClickListener(R.id.rLayout_device_item, deviceItemLongClickListener);
    
    
    @Override
    public boolean onLongClick(View v) {
        final int position = ViewUtils.getParentAdapterPosition(rVDevicesList, v, R.id.rLayout_device_item);
        return true;
    }
    

提交回复
热议问题