Android Build swipe ListView item from right to left show delete button (overlay on listview item)
我想创建滑动列表视图,当用户在列表视图项上从右向左滑动时,将向用户显示一些按钮选项。
如下图所示:
我已经看到一些滑动列表视图库,但这不是我所需要的。
有人可以帮我建议可以建立我的列表视图的图书馆吗?
谢谢。
我曾经遇到过与您相同的问题,我找不到可以滑动显示其他按钮的库,所以最终为自己编写了一个新库。签出我的图书馆:SwipeRevealLayout
对于您的特定布局,用法如下:
添加依赖项:
1 | compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.0.0' |
在您的row.xml文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <com.chauthai.swipereveallayout.SwipeRevealLayout android:layout_width="match_parent" android:layout_height="70dp" app:mode="normal" app:dragEdge="right"> <!-- Your delete and edit buttons layout here --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <!-- put your buttons here --> </LinearLayout> <!-- Your main layout here --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" /> </com.chauthai.swipereveallayout.SwipeRevealLayout> |
最后在您的适配器(RecyclerView或ListView)类中,当您绑定视图时,请使用ViewBinderHelper:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Adapter extends RecyclerView.Adapter { // This object helps you save/restore the open/close state of each view private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper(); @Override public void onBindViewHolder(ViewHolder holder, int position) { // get your data object first. YourDataObject dataObject = mDataSet.get(position); // Save/restore the open/close state. // You need to provide a String id which uniquely defines the data object. viewBinderHelper.bind(holder.swipeRevealLayout, dataObject.getId()); // do your regular binding stuff here } } |
您需要将GestureListener添加到列表视图单元格的主布局中。为此,请看以下内容:
https://developer.android.com/training/gestures/detector.html
此外,您需要将单元格的布局设置为RelativeLayout。这样,您就可以在彼此之上叠加项目。接下来,您需要做的是首先将这些叠加项的可见性设置为GONE并聆听用户的猛扑手势。如果检测到一个,则使重叠项目可见,以便用户可以选择它。
A.