SwipeLayout Framework Realizes Side pull Delete Edit Function

  • 2021-10-13 08:35:22
  • OfStack

In this paper, we share the specific code of SwipeLayout to realize side pull deletion editing for your reference. The specific contents are as follows

Step 1, add dependencies


dependencies {
  compile 'com.android.support:recyclerview-v7:21.0.0'
  compile 'com.android.support:support-v4:20.+'
  compile "com.daimajia.swipelayout:library:1.2.0@aar"
}

Step 2, Layout File


// It is best to suggest that BottomView Inside add layout_gravity Property, or add it in the code 
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp">
<!-- Bottom View Start-->
 <LinearLayout
  android:background="#66ddff00"
  android:id="@+id/bottom_wrapper"
  android:layout_width="160dp"
  android:weightSum="1"
  android:layout_height="match_parent">
  <!--What you want to show-->
  <!-- Write here the control that we show after sideslip -->
  <!- Usually a few TextView Or Button--->
</LinearLayout>
<!-- Bottom View End-->

<!-- Surface View Start -->
 <LinearLayout
  android:padding="10dp"
  android:background="#ffffff"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <!--What you want to show in SurfaceView-->
  <!-- Write here the layout of the contents displayed by our entries -->
</LinearLayout>
<!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>

Step 3, get the SwipeLayout instance in Activity.


SwipeLayout swipeLayout = (SwipeLayout) view.findViewById(R.id.swipelayout);
  // Set the mode of side pull 
  swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
  // If the layout file contains layout_gravity Property, this sentence can be ignored 
  //swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));

So far, the sideslip function of one item has been realized.
If we use this functionality in Listview, we need to inherit BaseSwipeAdapter and override several of its methods:


// Get swipeLayout Layout 
 @Override
public int getSwipeLayoutResourceId(int position) {
  return R.id.swipelayout;
}

// Generate the entry layout, which is equivalent to BaseAdapter Inside getView() Method 
@Override
public View generateView(int position, ViewGroup parent) {
  View view = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
  SwipeLayout swipeLayout = (SwipeLayout) view.findViewById(R.id.swipelayout);
  // Object for the control in the layout 1 Some initialization 
  swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);    
  return view;
}
// Assign a value to the control in the entry 
@Override
public void fillValues(int position, View convertView) {

}
// The following methods are BaseAdapter The method inside, BaseSwipeAdapter Is also inherited from BaseAdapter
@Override
public int getCount() {
  return 0;
}

@Override
public Object getItem(int i) {
  return null;
}

@Override
public long getItemId(int i) {
  return 0;
}

In this way, the entry sideslip of one ListView is basically realized. Click to delete/edit the code we implemented in the method generateView (). The following is an entry click event for Listview:


// Here swipeLayout Yes generateView() Obtained from the entry layout 
swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

If we need to implement other logic during sideslip, we can add sideslip monitoring:


swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
 @Override
 public void onClose(SwipeLayout layout) {
  //when the SurfaceView totally cover the BottomView.
 }

 @Override
 public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
  //you are swiping.
 }

 @Override
 public void onStartOpen(SwipeLayout layout) {

 }

 @Override
 public void onOpen(SwipeLayout layout) {
  //when the BottomView totally show.
 }

 @Override
 public void onStartClose(SwipeLayout layout) {

 }

 @Override
 public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
  //when user's hand released.
 }
});

At this point, the sideslip function of ListView is basically realized.


Related articles: