在onBindViewHolder RecyclerView

时间:2015-10-30 21:16:57

标签: android android-recyclerview swipe

嘿家伙我试图在onBindViewHolder中使用滑动,因为我的项目来自数据库,但我认为它似乎没有工作因为我的应用程序崩溃了。我在我的recyclerview https://gist.github.com/skyfishjy/443b7448f59be978bc59使用这个自定义游标适配器,这是我的代码。

@Override
public void onBindViewHolder(ItemViewHolder viewHolder, Cursor cursor) {
        mItems = cursor;
        final int     id      = cursor.getInt(cursor.getColumnIndex(MyDBHandler.COLUMN_ID));
        final String  title   = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_TITLE_REMINDER));
        final String  desc    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DESC_REMINDER));
        final String  date    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DATE_REMINDER));

        viewHolder.title.setText(title);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, String.valueOf(id), Toast.LENGTH_SHORT).show();
            }
        });

        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = 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 swipeDir) {
                int itemPosition = viewHolder.getAdapterPosition();
                notifyItemRemoved(itemPosition);
                dbHandler.deleteReminder(id);
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(this.myRecyclerview);
}

无论如何,我可以刷卡以在recyclerview中解雇我的物品吗?

3 个答案:

答案 0 :(得分:1)

从onBindViewHolder中删除以下代码块,并从初始化适配器的位置添加您的活动或片段。

 ItemTouchHelper.SimpleCallback simpleItemTouchCallback = 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 swipeDir) {     // type cast your view holder
                // CusrsorViewHolder cViewHolder = (CursorViewHolder)viewHolder;
                int itemPosition = viewHolder.getAdapterPosition();
                notifyItemRemoved(itemPosition);
                dbHandler.deleteReminder(cViewHolder.id);
            }
        };

 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
 itemTouchHelper.attachToRecyclerView(this.myRecyclerview);
 //put the above code before the following method in your activity or fragment
 //this.RecylerView.setAdapter(adapter)

在onBindViewHolder

中设置您的ID
@Override
public void onBindViewHolder(ItemViewHolder viewHolder, Cursor cursor) {
        mItems = cursor;
        final int     id      = cursor.getInt(cursor.getColumnIndex(MyDBHandler.COLUMN_ID));
        viewHolder.id = id;
}

答案 1 :(得分:0)

使用以下代码从列表中删除该项目。 orderlist是RecyclerView变量。

SwipeDismissRecyclerViewTouchListener touchListener =
    new SwipeDismissRecyclerViewTouchListener(
            orderlist,
            new SwipeDismissRecyclerViewTouchListener.DismissCallbacks() {
                @Override
                public boolean canDismiss(int position) {
                    return true;
                }

                @Override
                public void onDismiss(RecyclerView recyclerView, int[] reverseSortedPositions) {
                    for (int position : reverseSortedPositions) {
                        /*db.deleteOrderProduct(ordereditems.get(position).getOrder_pro_Code());
                        if(position>1){
                            listposition = position-1;
                        }*/
                        //ordereditems.remove(position);
                        //adapter.notifyItemRemoved(position);
                        //adapter.notifyDataSetChanged();
                        //displayOrderList();
                        DeleteOrderById(position);

                    }
                    adapter.notifyDataSetChanged();
                }
            });
orderlist.setOnTouchListener(touchListener);

答案 2 :(得分:0)

感谢@Waleed Sarwar帮助我解决问题。

我正在使用https://gist.github.com/skyfishjy/443b7448f59be978bc59作为我的recyclerviewcursoradapter。

MainActivity.java

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = 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 swipeDir) {
            RemindersAdapter.ItemViewHolder itemViewHolder = (RemindersAdapter.ItemViewHolder)viewHolder;
            int itemPosition = itemViewHolder.getAdapterPosition();
            adapter.notifyItemRemoved(itemPosition);
            // get the id of an item via itemViewHolder.id
            dbHandler.deleteReminder(itemViewHolder.id);

            // update cursor upon deleting do avoid
            // the card from coming back upon swipe
            adapter.swapCursor(dbHandler.getAllReminders());
        }
    };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(listReminder);

RemindersAdapter.java

@Override
public void onBindViewHolder(ItemViewHolder viewHolder, Cursor cursor) {
    final int     id      = cursor.getInt(cursor.getColumnIndex(MyDBHandler.COLUMN_ID));
    final String  title   = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_TITLE_REMINDER));
    final String  desc    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DESC_REMINDER));
    final String  date    = cursor.getString(cursor.getColumnIndex(MyDBHandler.COLUMN_DATE_REMINDER));

    viewHolder.title.setText(title);

    // pass id to viewholder to get in swipe
    viewHolder.id = id;
}

public class ItemViewHolder extends RecyclerView.ViewHolder{
    public int id;
    TextView title;

    public ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.reminderTitle);
        }
}

我正在使用adapter.swapCursor(dbHandler.getAllReminders());来自我的适配器,这是一个来自抽象类CursorRecyclerViewAdapter的方法,它在删除时更新游标,因为没有它,删除的项目将由onBindViewHolder重新创建。