国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

SwipeLayout框架實現側拉刪除編輯功能

2019-10-21 21:41:09
字體:
來源:轉載
供稿:網友

本文實例為大家分享了SwipeLayout實現側拉刪除編輯的具體代碼,供大家參考,具體內容如下

第一步、添加依賴

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"}

第二步、布局文件

//建議最好是在BottomView里面添加layout_gravity屬性,或者在代碼里面添加<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-->  <!--在這里寫我們側滑后顯示出來的控件-->  <!-通常是幾個TextView或者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-->  <!--這里寫我們條目顯示的內容的布局--></LinearLayout><!-- Surface View End --></com.daimajia.swipe.SwipeLayout>

第三步、在Activity中得到SwipeLayout實例.

SwipeLayout swipeLayout = (SwipeLayout) view.findViewById(R.id.swipelayout);  //設置側拉的模式  swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);  //如果布局文件里面有layout_gravity屬性,這句可以忽略  //swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));

到此為止,一個條目的側滑功能就實現了。 
如果我們在Listview里面使用這個功能,那么我們需要繼承BaseSwipeAdapter,并且復寫里面的幾個方法:

//獲取swipeLayout布局 @Overridepublic int getSwipeLayoutResourceId(int position) {  return R.id.swipelayout;}//生成條目布局,相當于BaseAdapter里面的getView()方法@Overridepublic 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);  //這里寫對布局中控件的一些初始化  swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);      return view;}//為條目里面的控件賦值@Overridepublic void fillValues(int position, View convertView) {}//下面的幾個方法是BaseAdapter里面的方法,BaseSwipeAdapter也是繼承自BaseAdapter@Overridepublic int getCount() {  return 0;}@Overridepublic Object getItem(int i) {  return null;}@Overridepublic long getItemId(int i) {  return 0;}

這樣,一個ListView的條目側滑就基本實現了。點擊刪除/編輯的代碼我們在方法generateView()里面實現。下面為Listview添加條目點擊事件:

//此處的swipeLayout是generateView()里面從條目布局里面獲取的swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {    }});

如果我們需要在側滑的時候實現其他邏輯的話,我們可以添加側滑監聽:

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. }});

到此,ListView側滑功能基本實現。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文成县| 呼玛县| 多伦县| 科尔| 清丰县| 公安县| 泸西县| 新竹市| 江城| 富宁县| 鹿邑县| 西乌珠穆沁旗| 达拉特旗| 宝鸡市| 龙岩市| 类乌齐县| 车致| 讷河市| 锦屏县| 焦作市| 团风县| 资中县| 郯城县| 郧西县| 宁波市| 手游| 阿拉尔市| 哈尔滨市| 台北县| 贵溪市| 高邑县| 镇平县| 滨州市| 旬邑县| 呼和浩特市| 棋牌| 洮南市| 桦川县| 赞皇县| 黄梅县| 和林格尔县|