RecyclerView在下方滑动查看
我一直在做一些研究,我还没有find一个例子或实现,允许您在滑动时在RecyclerView下面放置一个视图(下图中的示例)。 有没有人有任何例子或如何这将实施而不使用库的想法。
这是我如何实施刷卡解雇。
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 int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { return super.getSwipeDirs(recyclerView, viewHolder); } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { if (viewHolder instanceof ViewDividers) { Log.e("DIRECTION", direction + ""); } } }; new ItemTouchHelper(simpleItemTouchCallback).attachToRecyclerView(recycler);
以下是Google收件箱的示例:
我对如何完成的理解是,你会在xml中放置两个视图,这些视图将在你的recyclerview中按行显示。
例如,这将是我的适配器:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public static class ExampleViewHolder extends RecyclerView.ViewHolder { public TextView background; public TextView foreground; public ExampleViewHolder(View v) { super(v); background = (TextView) v.findViewById(R.id.background); foreground = (TextView) v.findViewById(R.id.foreground); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { if (holder instanceof ExampleViewHolder) { ((ExampleViewHolder) holder).background.setBackgroundColor(); // do your manipulation of background and foreground here. } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.example, parent, false); return new ExampleViewHolder(v); } } }
recyclerview中的每一行都是从R.layout.example中拉出xml布局。 因此,要在下面创build一个视图,可以使用relativelayout或framelayout来创build视图:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/background" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/foreground"/> </RelativeLayout>
那么如果你不想使用一个图书馆的刷卡,你可以从谷歌复制这个类,随后修改布鲁诺罗梅努奈斯:
该类将要求您创build一个滑动侦听器:
swipeTouchListener = new SwipeableRecyclerViewTouchListener(mRecyclerView, new SwipeableRecyclerViewTouchListener.SwipeListener() { @Override public boolean canSwipe(int position) { if (position == totalPost.size() - 1 && !connected) { return false; } return true; } @Override public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { //change some data if you swipe left } myAdapter.notifyDataSetChanged(); } @Override public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { //change some data if you swipe right } myAdapter.notifyDataSetChanged(); } });
然后简单地将它与您的recyclerview链接起来:
mRecyclerView.addOnItemTouchListener(swipeTouchListener);
我正在调查同样的问题。
我最终做的是,在包含recyclerView的布局中,我添加了一个名为“swipe_bg”的高度与我的RecyclerView.ViewHolder
项目相同的简单的FrameLayout
。 我将其可见性设置为“不见了”,并将其置于RecyclerView
之下
然后在我设置ItemTouchHelper
活动中,我重写了onChildDraw像这样..
ItemTouchHelper.SimpleCallback swipeCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) { @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { View itemView = viewHolder.itemView; swipe_bg.setY(itemView.getTop()); if (isCurrentlyActive) { swipe_bg.setVisibility(View.VISIBLE); } else { swipe_bg.setVisibility(View.GONE); } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } };
不知道这是不是最好的方法,但似乎是最简单的方法。
我喜欢@erik的方法,但我会build议通过传递的canvas来绘制你想要的onChildDraw()函数。 例如项目背景或图标。
@Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { final ColorDrawable background = new ColorDrawable(Color.RED); background.setBounds(0, itemView.getTop(), itemView.getLeft() + dX, itemView.getBottom()); background.draw(c); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); }
在这种方法中,您将根据需要绘制需要的内容,而不需要夸大未使用的视图
您可以使用RvTools库 。 它会帮助你轻松实现你想要的。
只需用期望的外观创build您的SwipeContextMenuDrawer
public class YourSwipeContextMenuDrawer extends SwipeContextMenuDrawer { private final Paint mRightPaint; private final Paint mIconPaint; private final Bitmap mRightIconBitmap; public YourSwipeContextMenuDrawer(@NonNull Context context) { mRightPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mRightPaint.setColor(ContextCompat.getColor(context, R.color.deep_orange_400)); mRightIconBitmap = GraphicUtils.getBitmap(context, R.drawable.ic_delete, 100, 100); mIconPaint = new Paint(); mIconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(context, R.color.backgroundColor), PorterDuff.Mode.SRC_IN)); } @Override public void drawRight(@NonNull Canvas canvas, @NonNull View view) { canvas.drawRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), mRightPaint); canvas.drawBitmap(mRightIconBitmap, view.getLeft() + mRightIconBitmap.getWidth() - 20, (view.getBottom() + view.getTop() - mRightIconBitmap.getHeight()) >> 1, mIconPaint); } @Override public void drawLeft(@NonNull Canvas canvas, @NonNull View view) { } }
并使用滑动动作创buildRvTools实例,并像这样滑动上下文菜单抽屉
new RvTools.Builder(recyclerView) .withSwipeRightAction(this) .withSwipeContextMenuDrawer(new YourSwipeContextMenuDrawer(getContext())) .buildAndApplyToRecyclerView();
简单的解决scheme,无需分配或canvas SomeAdapter.SomeVH
应包含上视图和下视图。 使用这种方法,我们将只能滑动上方视图(容器),暴露在视图之下(无论你想要的标签,图标)
class SomeTouchHelper extends ItemTouchHelper.Callback { ... @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { if (viewHolder instanceof SomeAdapter.SomeVH) { SomeAdapter.SomeVH someViewHolder = (SomeAdapter.SomeVH) viewHolder; ViewCompat.setTranslationX(someViewHolder.mContainer, dX); } } else { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } } ... }
不要忘记在适配器onViewRecycled()中恢复SomeVH
初始视图状态