第一步效果圖
1.0自定義控件 SwipeLayout 繼承FrameLayout重寫里面三個構造方法,分別調用initView().
2.0在布局中使用自定義控件
3.0在initView()方法中,創建拖拽輔輔助工具 ViewDragHelper()
該方法需要傳入回調 MyCallBack()
4.0,創建MyCallBack()回調,繼承ViewDragHelper.Callback
在回調中 覆蓋tryCaptureView方法,返回true 允許child被拖拽,被 覆蓋clampViewPositionHorizontal 返回left系統提供拖拽位置
5.0 onInterceptTouchEvent 返回:讓ViewDragHelper判斷是否需要攔截事件
6.0 onTouchEvent 返回true 并且讓ViewDragHelper分析事件
具體代碼:
布局:
<cn.itheima.swipelayout.SwipeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="wrap_content"><!--正文部分--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#fff"android:orientation="horizontal"><TextViewandroid:id="@+id/item_tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="張三"android:textSize="20sp"/></RelativeLayout><!--按鈕部分--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#888888"android:padding="10dp"android:text="呼叫"android:textSize="20sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#f00"android:padding="10dp"android:text="刪除"android:textSize="20sp"/></LinearLayout></cn.itheima.swipelayout.SwipeLayout>
SwipeLayout 代碼:
publicclassSwipeLayoutextendsFrameLayout{privateViewDragHelpermDragHelper;publicSwipeLayout(Contextcontext){super(context);initView();}publicSwipeLayout(Contextcontext,AttributeSetattrs){super(context,attrs);initView();}publicSwipeLayout(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);initView();}privatevoidinitView(){mDragHelper=ViewDragHelper.create(this,newMyCallBack());}//讓ViewDragHelper就是拖拽輔助工具返回true則表示要攔截觸摸事件@OverridepublicbooleanonInterceptTouchEvent(MotionEventev){//讓拖拽輔助工具判斷是否需要攔截事件returnmDragHelper.shouldInterceptTouchEvent(ev);}@OverridepublicbooleanonTouchEvent(MotionEventevent){//讓拖拽輔助工具分析事件分析用戶手勢mDragHelper.processTouchEvent(event);returntrue;}privateclassMyCallBackextendsViewDragHelper.Callback{/***如果返回true則表示child允許被拖拽*/@OverridepublicbooleantryCaptureView(Viewchild,intpointerId){returntrue;}/***固定被拖拽控件的水平位置,*參數里的left是系統推薦移動到的位置,可以進行修正,*方法返回的值就是child將要移動到的位置*/@OverridepublicintclampViewPositionHorizontal(Viewchild,intleft,intdx){returnleft;}}}
第二步:
1.0創建onFinishInflate方法獲取子控件,并且判斷健壯性
/*控件初始化時執行,可以用于獲取子控件*/@OverrideprotectedvoidonFinishInflate(){//健壯性檢查if(getChildCount()!=2){thrownewRuntimeException("SwipeLayout必須存放兩個子控件");}if(!(getChildAt(0)instanceofViewGroup)||!(getChildAt(1)instanceofViewGroup)){thrownewRuntimeException("SwipeLayout的子控件必須是ViewGroup");}mContent=(ViewGroup)getChildAt(0);mDeletePanel=(ViewGroup)getChildAt(1);}
2.0創建onSizeChanged方法,在控件大小改變的時候調用,獲取控件的寬高,和刪除的面板的最大移動范圍
/***當控件大小改變的時候調用這個方法*/@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);intmWith=w;intmHeigth=h;//界面創建過程中,不能使用getWidth方法intmRang=mDeletePanel.getMeasuredWidth();}
3.0在onLayout中指定側拉面板的位置
//指定側拉面板的位置@OverrideprotectedvoidonLayout(booleanchanged,intleft,inttop,intright,intbottom){super.onLayout(changed,left,top,right,bottom);mDeletePanel.layout(mWith,0,mWith+mRang,mHeigth);}
4.0在onViewPositionChanged方法中實現聯動效果
/***當被拖拽的控件已經移動過后,會調用這個方法,可以用于處理控件間的聯動效果*@left被拖拽控件的真實移動位置*@dx被拖拽控件的真實偏移大小*/@OverridepublicvoidonViewPositionChanged(ViewchangedView,intleft,inttop,intdx,intdy){if(changedView==mContent){//移動正文的同時也要移動側欄mDeletePanel.offsetLeftAndRight(dx);}else{mContent.offsetLeftAndRight(dx);}}
5.0在 clampViewPositionHorizontal方法中 固定被拖拽控件的水平位置,
/***固定被拖拽控件的水平位置,*參數里的left是系統推薦移動到的位置,可以進行修正,*方法返回的值就是child將要移動到的位置*/@OverridepublicintclampViewPositionHorizontal(Viewchild,intleft,intdx){if(child==mContent){if(left>0){left=0;}elseif(left<-mRang){left=-mRang;}}else{if(left>mWith){//mWith是屏幕的寬度left=mWith;}elseif(left<mWith-mRang){left=mWith-mRang;}}returnleft;}
第三步:
效果圖
1.0onViewReleased中根據來開局里面,判斷是否打開還是關閉
2.0 在 moveContent中第一次滑動
3.0computeScroll中,繼續滑動,直到滑動到指定的位置
4.0注意在onViewPositionChanged中手動刷新界面,調用invalidate方法
如果不手動刷新界面,效果展示不出來
/***當用戶松手時執行*@xvel松手時在X方向的移動速度,如果為正數則說明是向右移動,如果是負數則說明是向左移動,如果為零,說明是靜止狀態*/@OverridepublicvoidonViewReleased(ViewreleasedChild,floatxvel,floatyvel){if(xvel>0){//向右移動close();}elseif(xvel<0){//向左移動opend();}elseif(xvel>-mRang/2){//靜止狀態close();//展開不到一半,關閉面板}else{opend();}}}/***打開面板*/privatevoidopend(){intleft=-mRang;moveContent(left);}/***關閉面板*/privatevoidclose(){intleft=0;moveContent(left);}privatevoidmoveContent(intleft){//開啟平滑滾動,如果返回true則說明要繼續刷新界面,保持滾動if(mDragHelper.smoothSlideViewTo(mContent,left,0)){invalidate();}}@OverridepublicvoidcomputeScroll(){//繼續平滑滾動,如果返回true則說明要繼續刷新界面,保持滾動if(mDragHelper.continueSettling(true)){invalidate();}}
第四步:
1.0現給ListView賦值 在這就省略
2.0在SwipeLayout中使用枚舉記錄面板的狀態
privateenumStatus{CLOSED,OPENED,DRAGING;}privateStatusstatus=Status.CLOSED;publicStatusgetStatus(){returnstatus;}
3.0// 記錄上一個打開的面板。注意:一定要是 靜態變量
privatestaticSwipeLayoutpreSwipeLayout;
4.0在onViewPositionChanged中創建一個方法操作關閉面板
//關閉上一個打開的面板closePre();
5.0closePre()在這個方法中,判斷當前面板的狀態,并且根據狀態,關閉上一個打開的面板
//判斷當前面板是否正在打開,如果正在打開則將上一個打開的面板關閉privatevoidclosePre(){//記錄舊狀態StatuspreStatus=status;if(mContent.getLeft()==-mRang){//記錄當前面板已經打開status=status.OPENED;}elseif(mContent.getLeft()==0){//當前面板已經關閉status=status.CLOSED;}else{status=status.DRAGING;}//如果當前面板舊狀態為關閉,并且新狀態為拖拽,那么此時可以關閉之前打開的面板if(preStatus==status.CLOSED&&status==status.DRAGING){if(preSwipeLayout!=null&&preSwipeLayout!=this){//關閉上一個面板preSwipeLayout.close();}//將當前面板標記為打開的面板preSwipeLayout=this;}}
以上就是Android中怎么實現條目拖拽刪除功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注本站行業資訊頻道。
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
什么是證券的交易手續費交易手續費一般指證券 ,的交易手續費即證券公司在證券交易所交易成交后,按照成交實際金額的一定比例支付給證券交易所的交易費用。證券的交易成本一般包括印花稅和傭金。印花稅是指根據國家稅法規定,買賣雙方在股票成交后要繳納一定比例的稅款,印花稅一般由證券營業機構扣除。傭金是指在證券交易完成后,投資者支付給證券公司的一定比例的費用。 傭金一般由證券公司,經紀傭金、證券交易所手續費和管理...
每克。目前白銀最新報價為人民幣/克23一般回收會折價6成左右。s999純銀產品分為銀料價格和銀的手工費兩部分,基本上,999千足銀大概17,一般都在10幾塊錢到30多塊之間。銀飾品是不按照克來計算價格的,你好,因此都是按件”賣,工藝非常的美,可能是品牌價值高的緣故吧。這種價格為加工,可能會達到30左右每克,加工費根據銀的工藝不同價格不同。999才是足銀,至于你說的30元,比如今天國際黃...
匯率對生活的影響還是有的,歐元兌美元匯率在過去一年多的時間里連續下跌。這個情況讓很多投資者都擔心,不少投資者還直接將資金撤了回來。如果歐元匯率繼續下探,歐元與美元平價的可能性將大大增加。歐元匯率為什么跌得這么厲害?歐元匯率一直跌最直接導火索是俄烏沖突,這場發生在歐盟家門口的戰爭讓歐洲經濟再一次陷入險境。更何況,由于疫情影響已經處于高位的通貨膨脹,在美國的節奏帶動下出現嚴重的社會共振。再加上此前的歐...