本文實例為大家分享了SwipeLayout實現(xiàn)側(cè)拉刪除編輯的具體代碼,供大家參考,具體內(nèi)容如下

10年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站建設(shè),個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁制作,對成都人造霧等多個領(lǐng)域,擁有多年的營銷推廣經(jīng)驗。
第一步、添加依賴
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--> <!--在這里寫我們側(cè)滑后顯示出來的控件--> <!-通常是幾個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--> <!--這里寫我們條目顯示的內(nèi)容的布局--> </LinearLayout> <!-- Surface View End --> </com.daimajia.swipe.SwipeLayout>
第三步、在Activity中得到SwipeLayout實例.
SwipeLayout swipeLayout = (SwipeLayout) view.findViewById(R.id.swipelayout); //設(shè)置側(cè)拉的模式 swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown); //如果布局文件里面有l(wèi)ayout_gravity屬性,這句可以忽略 //swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));
到此為止,一個條目的側(cè)滑功能就實現(xiàn)了。
如果我們在Listview里面使用這個功能,那么我們需要繼承BaseSwipeAdapter,并且復(fù)寫里面的幾個方法:
//獲取swipeLayout布局
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipelayout;
}
//生成條目布局,相當(dāng)于BaseAdapter里面的getView()方法
@Override
public 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;
}
//為條目里面的控件賦值
@Override
public void fillValues(int position, View convertView) {
}
//下面的幾個方法是BaseAdapter里面的方法,BaseSwipeAdapter也是繼承自BaseAdapter
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}這樣,一個ListView的條目側(cè)滑就基本實現(xiàn)了。點擊刪除/編輯的代碼我們在方法generateView()里面實現(xiàn)。下面為Listview添加條目點擊事件:
//此處的swipeLayout是generateView()里面從條目布局里面獲取的
swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});如果我們需要在側(cè)滑的時候?qū)崿F(xiàn)其他邏輯的話,我們可以添加側(cè)滑監(jiān)聽:
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側(cè)滑功能基本實現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:SwipeLayout框架實現(xiàn)側(cè)拉刪除編輯功能
文章網(wǎng)址:http://www.chinadenli.net/article46/isjdhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、面包屑導(dǎo)航、微信公眾號、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)