RecyclerView插入/删除动画会删除不需要的对象 [英] RecyclerView insert /remove animation deletes unwanted object

查看:509
本文介绍了RecyclerView插入/删除动画会删除不需要的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请朋友们帮助我,我被困在解决这个RecyclerView问题上.我是RecyclerView的新手,并创建了一个RecyclerView,它只是具有一个类似于待办事项的应用程序来管理任务.

Please friends help me I am Stuck in solving this RecyclerView Problem. I am new to RecyclerView and created a RecyclerView that simply have a todo-like application for managing tasks.

问题是:

  • 当我删除任何项目并调用notifyItemRemoved()时,它有时会删除不需要的项目,并且应用程序崩溃. (可能是因为索引啮合了)
  • 即使我再次调用setAdapter进行更新,也会发生不希望的事情(请参见代码中的#here标记.).颜色会在开始时设置为所需的项目,但向下滚动后,该颜色将应用于所有向上滚​​动的项目.
  • When i remove any item and call notifyItemRemoved() it sometime deletes undesired item and the app crashes. (may be because indexes mesh up)
  • Even when i update by again calling setAdapter, undesired things happen(see #here mark in code.). The color is set to desired items on start but after scrolling down it applies to all those are scrolled up.

这是我的适配器:

public class RVadapter extends RecyclerView.Adapter<RVadapter.mHolder> {

    public List<Task> mTask;
    private SQLhelper sql;
    private Context mContext;
    public RVadapter(Context context)
    {
        super();
        mContext=context;
        sql=new SQLhelper(mContext,null);
        PlannerAI ai=new PlannerAI();
        mTask=ai.generateTaskList(sql.getTasks());
    }

    public interface listener {
        public void refresh();
    }

    @Override
    public void onBindViewHolder(mHolder holder,final int i) {
        try {
            final Task t = mTask.get(i);
            // #here
            if(t.getId().equals("Enjoy"))
            {
               holder.vv.setBackgroundColor(mContext.getResources().getColor(android.R.color.some_color)); 
            }
            holder.name.setText(t.getName());
            holder.extras.setText(t.getExtras());
            holder.id.setText(t.getId());
            holder.subject.setText(t.getSubject());
            holder.type.setText(t.getType());
            holder.noq.setText(t.getNo()+" questions");
            holder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(t.id!=null) {
                        sql.deleteTask(t.id);
                        mTask.remove(i);
                        notifyItemRemoved(i);
                        if (mContext instanceof listener) {
                            ((listener) mContext).refresh();
                        }
                    }
                }
            });
        }catch(Exception e){
            Log.d("TAG_LOG_EM",e.toString());
        }
    }


    @Override
    public mHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       mHolder view;
        view = null;
        try{
            View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.planner_item,null);
            view=new mHolder(v);

       }catch(Exception e){
           Log.d("TAG_LOG_EM",e.toString());
       }
        return view;
    }

    @Override
    public int getItemCount() {
        return mTask.size();
    }

    class mHolder extends RecyclerView.ViewHolder{
        public TextView name;
        public TextView extras;
        public TextView id;
        public TextView subject;
        public TextView type;
        public TextView noq;
        public View vv;
        public ImageView del;

        public mHolder(View view){
            super(view);
            vv=view;
            try {
                name = (TextView) view.findViewById(R.id.planner_name);
                extras = (TextView) view.findViewById(R.id.planner_extras);
                id = (TextView) view.findViewById(R.id.planner_id);
                del = (ImageView) view.findViewById(R.id.t_delete);
                subject = (TextView) view.findViewById(R.id.planner_subject);
                type = (TextView) view.findViewById(R.id.planner_type);
                noq = (TextView) view.findViewById(R.id.planner_no);
            }catch(Exception e){
                Log.d("TAG_LOG_EM",e.toString());
            }
        }

    }
}

我不需要SQL和MainActivity类(如果需要,请注释). 请帮助并告知任何建议的代码更改. 谢谢!

I don't the SQL and MainActivity classes are needed(please comment if needed). Please Help and tell any recommended changes in code. Thanks!

刷新功能为:

public void refresh(){

        radapter = new RVadapter(this);
        rv.setAdapter(radapter);
    }

推荐答案

只需将这些函数放入RVadapter

public void onItemDismiss(int position) {
    if(position!=-1 && position<mTask.size())
    {
        mTask.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount());
    }
}

它提供了适配器中当前存在的总元素数

it provides the count of total elements currently present in the adapter

@Override
public int getItemCount() {
    return (null != mTask ? mTask.size() : 0);
}

,然后在您的onClick()中添加:

and in your onClick(), just add :

holder.del.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
          onItemDismiss(i)
    }
});

说明

mTask.remove(position);-从列表中移除特定位置的元素.

mTask.remove(position); - removes the element at particular position from the List.

notifyItemRemoved(position);-通知RecyclerView Adapter适配器中的数据已在特定位置删除.

notifyItemRemoved(position); - notifies the RecyclerView Adapter that data in adapter has been removed at a particular position.

notifyItemRangeChanged(position, getItemCount());-通知RecyclerView Adapter适配器中元素的位置已从位置(已从元素索引删除到列表末尾)更改,请进行更新.

notifyItemRangeChanged(position, getItemCount()); - notifies the RecyclerView Adapter that positions of element in adapter has been changed from position(removed element index to end of list), please update it.

if(position!=-1 && position<mTask.size())-此条件验证元素的位置不等于-1,并且元素的位置应小于列表中总元素的大小.因此不会由于元素索引而导致意外崩溃.

if(position!=-1 && position<mTask.size()) - this condition verifies that the position of an element is not equal to -1 and the position of an element should be less than the size of total elements in the list. hence not causing an unwanted crash due to indexes of elements.

这篇关于RecyclerView插入/删除动画会删除不需要的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆