ASwipeLayout一個強大的側滑選單控制元件
阿新 • • 發佈:2019-01-10
前言
該控制元件的優點:
- 1.無論是在RecyclerView,ListView,還是LinearLayout等,只要是ViewGroup用該控制元件都能實現側滑。
- 2.控制元件的手勢滑動衝突已解決,不會出現巢狀到ScrollView等控制元件出現滑動不流暢的情況
- 3.控制元件使用簡單,只需要在xml外套一層該控制元件就好了,秒接入
- 4.點選事件很方便,原來什麼寫法就什麼寫法
1.效果圖
2.使用方式其實挺簡單的,在設計的時候,就是想著怎麼簡單怎麼來
2.1引入庫:
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在需要側滑的佈局的根佈局中新增下面這段程式碼,注意註釋的地方才是可以定製的:
<?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>
注意在這裡ll_content,right_menu_content是一定要的,這個id對應的佈局不要自己去改變,以後有需要會放開,目前的話,一般的情況你們只需要定製主item的內容和右側選單欄了,在這裡我也省去了定義一些額外的自定義view了,單純就是用id,來區分主item和右側的選單。
3.因為RecyclerView中有複用Item的情況,針對這種情況的解決方案
因為item複用會使得當我們滑出某個menu的時候,再進行RecyclerView的上下滑動時,會使得其他的Item也滑出了menu,這就是item複用導致了佈局錯亂,所以針對這型別的問題的話,我在這裡已經提供了OnSwipeStateChangeListener介面,在這裡你們可以記錄下滑動的狀態,在onBindViewHolder方法裡面,根據狀態來設定Item是開啟menu還是關閉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);
}
});
}
如上程式碼就可以解決Item複用導致佈局錯亂的問題了(粑粑再也不用擔心RecyclerView複用的問題了)。