ASwipeLayout a powerful side sliding menu control

There are a lot of side-slip scenes in Android, most of which are based on RecyclerView, but sometimes you can dynamically addView to a layout, and you also want it to slide sideways, so ASwipeLayout is created, which not only supports implementation in RecyclerView side slip

In fact, as long as you wrap this layer of layout, you can achieve side sliding.

1. Rendering

double row

2. The use method is actually quite simple. When designing, I just thought about how to make it simple.

2.1 Import the library:

Step 1. Add it in your root build.gradle at the end of repositories:
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
Step 2. Add the dependency

    dependencies {
            implementation 'com.github.WelliJohn:ASwipeLayout:0.0.2'
    }

2.2 Add the following code to the root layout of the layout that needs to slide sideways. Note that the commented place can be customized:

<?xml version="1.0" encoding="utf-8"?>
<wellijohn.org.swipevg.ASwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="horizontal">

        //在这里是实现你的主item的东西,根据你们的项目随便添加
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_menu_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
 
        //在这里是实现右侧的菜单,根据你们的项目随便添加
    </LinearLayout>


</wellijohn.org.swipevg.SwipeLayout>

Note that ll_content and right_menu_content are required here. Do not change the layout corresponding to this id. You will release it if necessary. At present, in general, you only need to customize the content of the main item and the menu bar on the right. Here I also omitted to define some additional custom views, and simply used id to distinguish the main item from the menu on the right.

3. We know that ViewHolder is reused in RecyclerView, which will make other items slide out of menu when we slide out of a menu and then slide up and down of RecyclerView, which is item Multiplexing leads to data confusion, so for this type of problem, I have provided the OnSwipeStateChangeListener interface here, where you can record the sliding state, and in the onBindViewHolder method, according to the state, set the item whether to open the menu or close it menu:

 @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final Person person = mDatas.get(position);
        holder.scrollDelLl.setOpen(person.isOpen());

        holder.scrollDelLl.setOnSwipeStateChangeListener(new OnSwipeStateChangeListener() {
            @Override
            public void onSwipeStateChange(boolean open) {
                person.setOpen(open);
            }
        });

    }

The above code can solve the problem of disordered layout caused by Item reuse (Baba no longer has to worry about the reuse of RecyclerView).

4. In the current project, only the implementation of the menu on the right is supported, because I have not seen many needs on the left. Of course, if there is a need, you can add the following group to mention your needs or problems encountered. I will add or modify it when I have time:

5. The code has been uploaded to github, ASwipeLayout

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652326&siteId=291194637