ASwipeLayout一個強大的側滑菜單控件
阿新 • • 發佈:2018-04-20
側滑 Android Android中側滑的場景有很多,大部分是基於RecyclerView,但是有些時候你可以動態地addView到一個布局當中,也希望它實現側滑,所以就產生了ASwipeLayout,該控件不僅支持在RecyclerView中實現側滑
實際上只要你包裹了這層布局,都能實現側滑。
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中ViewHolder是有復用的情況,這樣會使得當我們滑出某個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復用的問題了)。
4.目前的項目當中,只是支持了右側菜單的實現,因為左側的需求我現在見到不是很多,當然如果有需求的話,可以在github上提你們的需求或者遇到的問題,我在有時間的情況會進行添加或者修改:
5.代碼已上傳github,ASwipeLayout
ASwipeLayout一個強大的側滑菜單控件