当前位置: 首页 > 知识库问答 >
问题:

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

山阳辉
2023-03-14
 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);


    }

}

我试过了所有的方法,但是很难从RecolyerView中滑动项目,并且当snackbar的undo被按下时,我希望该项目重新出现。我希望从RecolyerView中滑动项目,并且当项目被滑动时,底部的项目应该向上移动。

共有1个答案

牛越
2023-03-14

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。

 类似资料:
  • 问题是将作为参数,根据文档,该参数是“RecyclerView正在绘制其子级的画布”。有没有办法从中获取画布?如果执行,则会执行代码,但在ViewWholder后面没有绘制任何内容,因为它显然不是recyclerview使用的画布...也许有办法设置一个canvas供recyclerview使用吗? 谢谢你的正手点子。

  • 我想使用swipe手势(即gmail应用程序)实现recyclerview项目之间的导航。这样,用户就不必返回列表,只需单击下一个项目就可以获得其详细信息视图。我找不到如何实现这一点的教程。

  • 我已经设置了一个回收器视图,每行都有文本视图和隐藏按钮。 如果用户单击该项,就会显示按钮。 如果用户单击其他项,则按钮将被隐藏。 再循环视图将位置发送到每个接口的活动,该活动更改其属性。 只要不滚动recyclerview,一切都能完美运行。 Layoutmanager.getchildat(职位) 现在,当项目超出视图时,用户滚动recyclerview,位置就会出错。每向下滚动一个用户单击的项

  • 我正在尝试为我的RecolyerView添加滑动功能。我正在跟随这个链接添加刷卡。https://github.com/nikhilpanju/recyclerviewenhanced/blob/master/recyclerviewenhanced/src/main/java/com/nikhilpanju/recyclerviewenhanced/recyclertouchlistener.j

  • Iam正在尝试编辑和删除RecyclerView中的项目。当我刷回收视图,我应该得到两个按钮。一个按钮用于编辑数据,另一个按钮用于从RecyclerView中删除数据。