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

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

问题描述

请朋友帮我解决这个RecyclerView 问题.我是 RecyclerView 的新手,并创建了一个 RecyclerView,它只有一个类似于 todo 的应用程序来管理任务.

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

Just put these functions inside your 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)
    }
});

编辑 1: 说明

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 - 这个条件验证一个元素的位置不等于 -1 和一个元素的位置element 应该小于列表中总元素的大小.因此不会因元素索引而导致不必要的崩溃.

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天全站免登陆