一個強大的側(cè)滑菜單控件ASwipeLayout
前言
該控件的優(yōu)點:
1.無論是在RecyclerView,ListView,還是LinearLayout等,只要是ViewGroup用該控件都能實現(xiàn)側(cè)滑。
2.控件的手勢滑動沖突已解決,不會出現(xiàn)嵌套到ScrollView等控件出現(xiàn)滑動不流暢的情況
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在需要側(cè)滑的布局的根布局中添加下面這段代碼,注意注釋的地方才是可以定制的:
<?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"> //在這里是實現(xiàn)你的主item的東西,根據(jù)你們的項目隨便添加 </LinearLayout> <LinearLayout android:id="@+id/right_menu_content" android:layout_width="wrap_content" android:layout_height="match_parent"> //在這里是實現(xiàn)右側(cè)的菜單,根據(jù)你們的項目隨便添加 </LinearLayout> </wellijohn.org.swipevg.SwipeLayout>
注意在這里ll_content,right_menu_content是一定要的,這個id對應的布局不要自己去改變,以后有需要會放開,目前的話,一般的情況你們只需要定制主item的內(nèi)容和右側(cè)菜單欄了,在這里我也省去了定義一些額外的自定義view了,單純就是用id,來區(qū)分主item和右側(cè)的菜單。
3.因為RecyclerView中有復用Item的情況,針對這種情況的解決方案
因為item復用會使得當我們滑出某個menu的時候,再進行RecyclerView的上下滑動時,會使得其他的Item也滑出了menu,這就是item復用導致了布局錯亂,所以針對這類型的問題的話,我在這里已經(jīng)提供了OnSwipeStateChangeListener接口,在這里你們可以記錄下滑動的狀態(tài),在onBindViewHolder方法里面,根據(jù)狀態(tài)來設定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.如果你們在項目使用的過程中,有新的需求或者是bug的話,可以在github上提你們的需求或者issue
5.代碼已上傳github,ASwipeLayout
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android實現(xiàn)可點擊的幸運大轉(zhuǎn)盤
這篇文章主要為大家詳細介紹了Android實現(xiàn)可點擊的幸運大轉(zhuǎn)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02詳解Android中的NestedScrolling機制帶你玩轉(zhuǎn)嵌套滑動
這篇文章主要給大家詳細解析了Android中的NestedScrolling機制,通過介紹該機制帶你玩轉(zhuǎn)Android中的嵌套滑動效果,文中給出了詳細的示例代碼和介紹,需要的朋友們可以參考學習,下面來一起看看吧。2017-05-05Android studio 實現(xiàn)隨機位置畫10個隨機大小的五角星的代碼
這篇文章主要介紹了Android studio 實現(xiàn)隨機位置畫10個隨機大小的五角星,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05