【问题标题】:how to swipe item in recyclerview when using contentproviders使用 contentproviders 时如何在 recyclerview 中滑动项目
【发布时间】:2017-08-12 17:20:21
【问题描述】:

我想从 recyclerview 中滑动项目,当项目被滑动时,底部的项目应该向上移动。

在我的 Fragment 的 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 中滑动项目,当项目被滑动时,底部项目应该向上移动。

【问题讨论】:

    标签: android android-recyclerview android-contentprovider swipe


    【解决方案1】:

    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) 要使底部项目向上移动(更新),您需要在“adapter.notifyDataSetChanged()”首先更新数据库后进行刷新,然后从数据库中提取新数据(有更改)。这将更新数据库和recyclerview。

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

    【讨论】:

    • 请检查我的问题,我已经覆盖了该方法,我无法使用 arraylist,因为我正在使用带有内容提供程序的 sqlite
    【解决方案2】:

    如果您使用的是 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;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2023-03-19
      • 2021-08-25
      • 2020-09-25
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多