尝试通过向左或向右滑动来删除RecycleView项(从webservices加载)

时间:2017-11-14 07:00:35

标签: android android-recyclerview swiperefreshlayout

我试图删除从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();
    }
}

我的Adapter类是

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();
    }
}

使用删除按钮时它工作正常,但通过滑动它无法正常显示空白区域并且适配器未正确更新。

我是新的滑动删除过程我正在学习它所以建议我可能的变化或建议我做任何最好的方法 The empty space is showing by swiping

我刷了两个列表项但空间正在显示..我已经尝试了所有

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

3 个答案:

答案 0 :(得分:2)

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;
    }

adapter.notifyDataSetChanged();

之后使用itemTouchHelper.attachToRecyclerView(rView)

请试试这个。快乐编码

答案 1 :(得分:1)

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

datetime

答案 2 :(得分:1)

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();

感谢您的支持