Android Build swipe ListView item from right to left show delete button (overlay on listview item)

瘦欲@ 提交于 2019-12-11 09:00:10

问题


I want to create swipe listview, when user swipe right to left on an listview item, user will be show some button options.

It look like below image :

I have been seen some swipe listview library, but it isn't what I need.

Can anyone help me suggest to me the library that can do build my listview?

Thanks.


回答1:


I used to have the same problem as your, I couldn't find a library to swipe to show other buttons so I ended up writing a new library for myself. Check out my library: SwipeRevealLayout

For your specific layout, the usage is the following:

Add dependencies:

compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.0.0'

In your row.xml file:

<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>

And finally in your adapter (RecyclerView or ListView) class, when you bind your view, use 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
  }
}



回答2:


You need to add GestureListener to the main layout of your listview cells. For this take a look at this: https://developer.android.com/training/gestures/detector.html

Also you need to set the layout of your cells as RelativeLayout. Doing this and you can overlay items on top of eachother. Next what you need to do is to set the visibility of these overlay items as GONE at first and listen to user fling gesture. If you detected one, make the overlay items VISIBLE so user can choose it.

A.



来源:https://stackoverflow.com/questions/30749822/android-build-swipe-listview-item-from-right-to-left-show-delete-button-overlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!