使用contentproviders时如何在recyclerview中滑动项目

时间:2017-08-12 17:20:21

标签: android android-recyclerview android-contentprovider swipe

我想从recyclerview中轻扫项目,当项目被刷过时,底部项目应该向上移动。

在片段的onCreateView中

 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) {
                //Toast.makeText(getActivity(),"ByBy",Toast.LENGTH_LONG).show();

                myListCursorAdapter.onItemRemove(viewHolder, recyclerView);
            }
        }).attachToRecyclerView(recyclerView);
适配器

中的

public class TaskCursorAdapter extends RecyclerCursorAdapter<TaskCursorAdapter.ViewHolder> {
    Context context;
    Cursor cursor;


    public TaskCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor);
        this.cursor = cursor;
        this.context = context;
        setHasStableIds(true);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView taskname;
        public TextView dateDetails;
        Context context;
        Cursor cursor;

        public ViewHolder(View view, Context context, Cursor cursor) {
            super(view);
            this.context = context;
            this.cursor = cursor;
            taskname = (TextView) view.findViewById(R.id.listitemtaskname);
            dateDetails = (TextView) view.findViewById(R.id.listitemdatedetails);
            view.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {

            // Form the content URI that represents the specific pet that was clicked on,
            // by appending the "id" (passed as input to this method) onto the
            // {@link PetEntry#CONTENT_URI}.
            // For example, the URI would be "content://com.example.android.pets/pets/2"
            // if the pet with ID 2 was clicked on.
            Intent intent = new Intent(context, EditorActivity.class);
            Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, this.getAdapterPosition() + 1);

            intent.setData(currentPetUri);
            context.startActivity(intent);


        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view, context, cursor);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {

        int taskColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.NAME);
        int dateColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.DUEDATE);
        int timeColumnIndex = cursor.getColumnIndex(TaskContract.TaskEntry.DUETIME);

        String task = cursor.getString(taskColumnIndex);
        String dateString = cursor.getString(dateColumnIndex);
        String timeString = cursor.getString(timeColumnIndex);

        viewHolder.cursor = cursor;
        viewHolder.taskname.setText(task);

        if (timeString == null && dateString == null) {
            viewHolder.dateDetails.setVisibility(View.GONE);

        } else {
            viewHolder.dateDetails.setText(dateString + "\t\t" + timeString);
        }

    }

    public void onItemRemove(final RecyclerView.ViewHolder viewHolder, final RecyclerView recyclerView) {
        int adapterPosition = viewHolder.getAdapterPosition()+1;
         Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, viewHolder.getAdapterPosition());


        Snackbar snackbar = Snackbar
                .make(recyclerView, "PHOTO REMOVED", Snackbar.LENGTH_LONG)
                .setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int mAdapterPosition = viewHolder.getAdapterPosition() + 1;
                        Toast.makeText(context,"Hi",Toast.LENGTH_LONG).show();

                        recyclerView.scrollToPosition(mAdapterPosition);

                    }
                });
        snackbar.show();
        context.getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI_FINISHED, values);
       // Toast.makeText(context,"Hi2",Toast.LENGTH_LONG).show();
        //notifyItemChanged(adapterPosition);
        recyclerView.scrollToPosition(adapterPosition);


    }

}

我尝试了所有的东西,但是很难从recyclerview上刷掉物品,当按下快餐栏的撤消时,我想要再次出现该物品。我想从recyclerview上刷物品,当物品被刷过时,底部物品应该向上移动。

2 个答案:

答案 0 :(得分:2)

1)您需要覆盖onSwiped()方法,如下所示。

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN | ItemTouchHelper.UP) {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
               //do something
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {      
                int position = viewHolder.getAdapterPosition();
                arrayList.remove(position);//remove swiped item
                adapter.notifyDataSetChanged();//notify the recyclerview changes in the dataset

            }
     };

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(rv);//attach to your recyclerview

2)不要忘记导入RecyclerView库的v22.2。+。

3)要使botom项向上移动(更新),您需要在“adapter.notifyDataSetChanged()”首先更新数据库之后进行刷新,然后从数据库中提取新数据(带有更改)。这将更新数据库以及recyclerview。

4)要撤消,您需要保留一堆已删除的元素(LIFO顺序),并在单击撤消时将其插回。

答案 1 :(得分:0)

如果您使用的是Content Provider,则必须在onSwiped方法中调用光标,如下所示:

 ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(
            0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {      
            final int position = viewHolder.getLayoutPosition();
            Cursor cursor = myListCursorAdapter.getCursor();
            cursor.moveToPosition(x);
                final long id = cursor.getLong(cursor.getColumnIndex(Your_Column_ID));
                getActivity().getContentResolver().delete(Uri.withAppendedPath(YourContract.YOUR_URI, String.valueOf(id)), null, null);
            }
 };
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(yourRecyclerView);          //attach to your recyclerview

在适配器中,必须设置getCursor():

   public Cursor getCursor(){
    return yourCursor;
  }