我想创建滑动列表视图,当用户在列表视图项目上从右向左滑动时,用户将显示一些按钮选项。
如下图所示:
我见过一些滑动列表视图库,但这不是我需要的。
任何人都可以帮我推荐可以构建我的列表视图的库吗?
谢谢。
我曾经遇到过和你一样的问题,我找不到一个库来滑动以显示其他按钮,所以我最终为自己编写了一个新库。查看我的库:SwipeRevealLayout
对于您的具体布局,用法如下:
添加依赖项:
compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.0.0'
在您的 row.xml 文件中:
<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:
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。执行此操作后,您可以将项目叠加在一起。接下来您需要做的就是首先将这些覆盖项的可见性设置为“消失”,并监听用户的滑动手势。如果您检测到一个,请将覆盖项目设为可见,以便用户可以选择它。
A.
implementation group: 'com.apachat', name: 'swipereveallayout-android', version: '1.1.2'
此依赖项还将包括滑动显示布局,因为最新版本的 gradle 不支持 - 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.0.0'
以下示例
<com.apachat.swipereveallayout.core.SwipeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipelayout"
app:dragEdge="right"
app:mode="same_level">
<!--Swipe Layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/like"
android:layout_margin="10dp"
android:text="Like"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/delete"
android:layout_margin="10dp"
android:text="Delete"/>
</LinearLayout>
<!--Main Layout-->
<RelativeLayout
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/swipeimg"
android:layout_marginEnd="20dp"
/>
<LinearLayout
android:layout_toEndOf="@id/swipeimg"
android:layout_alignBottom="@id/swipeimg"
android:id="@+id/linearswipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:layout_width="match_parent"
android:padding="10dp"
android:gravity="bottom"
android:layout_height="0dp"
android:fontFamily="sans-serif-condensed-medium"
android:layout_weight="1"
android:id="@+id/nameswipe"
android:textSize="22sp"
android:textColor="@color/white"
android:text="Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:textSize="16sp"
android:fontFamily="sans-serif-condensed-medium"
android:gravity="top"
android:layout_weight="1"
android:textColor="#8C8787"
android:id="@+id/byswipe"
android:text="singer"/>
</LinearLayout>
</RelativeLayout>
</com.apachat.swipereveallayout.core.SwipeLayout>