试图通过向左或向右滑动来删除 RecyclerView 项目(从网络服务加载)

Trying to remove a RecycleView item(Loaded from webservices) by swiping left or right

我试图删除从 Url 加载数据的 recyclerview 的特定列表项。

这是我的 MainActivity

    public class MainActivity extends AppCompatActivity {
    String responseFromServer;
    Button btnGetData;
    RecyclerView rView;
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnGetData = (Button)findViewById(R.id.btnGetData);
        rView = (RecyclerView)findViewById(R.id.rVIew);
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please Wait");
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        rView.setLayoutManager(manager);
        btnGetData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               refreshAdapter();
            }
        });

    }
    class PlacesTask extends AsyncTask<String,String,String>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            WebMethods methods = new WebMethods();
            responseFromServer = methods.getData();
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pDialog.dismiss();
            setOutData(responseFromServer);
        }
    }
    public void setOutData(String  response)
    {
        ParserClass parserClass = new ParserClass();
        ArrayList<PlacesData> data = parserClass.getData(response);
        final MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this,data);
        rView.setAdapter(adapter);


        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                rView.removeViewAt(viewHolder.getAdapterPosition());
               // adapter.notifyDataSetChanged();
             //   adapter.notifyItemRemoved(viewHolder.getAdapterPosition());

                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
               //adapter.notifyItemChanged(viewHolder.getAdapterPosition());
               // adapter.notifyItemRemoved(viewHolder.getAdapterPosition());



            }
        });
        itemTouchHelper.attachToRecyclerView(rView);
    }
    public void refreshAdapter()
    {
        PlacesTask task = new PlacesTask();
        task.execute();
    }
}

我的适配器 class 是

MyRecyclerAdapter.java

    public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecycler> {
    MainActivity activity;
    LayoutInflater inflater;
    ArrayList<PlacesData> data;
    public MyRecyclerAdapter(MainActivity activity,ArrayList<PlacesData> data)
    {
        inflater = LayoutInflater.from(activity);
        this.activity =activity;
        this.data = data;
    }
    @Override
    public MyRecycler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.singleitem,null);
        MyRecycler recycler = new MyRecycler(view);
        return recycler;
    }

    @Override
    public void onBindViewHolder(MyRecycler holder, final int position) {
        holder.tvName.setText(data.get(position).getName());
        holder.tvRating.setText(data.get(position).getRating());
        Picasso.with(activity).load(data.get(position).getIcon()).into(holder.imgIcon);
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.remove(data.get(position));
                notifyDataSetChanged();
            }
        });
    }

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

使用删除按钮时它工作正常,但滑动它不起作用它显示为空 space 并且适配器未正确更新。

我是这个滑动删除过程的新手,我正在学习它所以建议我可能的更改或建议我执行此操作的任何最佳方法

我刷了两个列表项,但显示 space.. 我已经尝试了所有

        adapter.notifyDataSetChanged();
        adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
        adapter.notifyItemChanged(viewHolder.getAdapterPosition());

ArrayList 中删除 onMove() 中的数据。使用notifyDataSetChanged()

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
       // rView.removeViewAt(viewHolder.getAdapterPosition());
        adapter.notifyItemRemoved(viewHolder.getAdapterPosition()); //Optional .Since we uses notifyDataSetChanged()
        data.remove(viewHolder.getAdapterPosition());

       // adapter.notifyDataSetChanged();
     //   adapter.notifyItemRemoved(viewHolder.getAdapterPosition());

        return false;
    }

itemTouchHelper.attachToRecyclerView(rView)

之后使用adapter.notifyDataSetChanged();

请试试这个。快乐编码

请在 onSwiped 方法中更改以下代码。

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
     adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
     list.remove(position);
     adapter.notifyDataSetChanged();
}
By These Changes I got the solution

 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            data.remove(data.get(viewHolder.getAdapterPosition()));
            adapter.notifyDataSetChanged();




        }
    });
    itemTouchHelper.attachToRecyclerView(rView);
    adapter.notifyDataSetChanged();

感谢您的支持