本篇內容主要講解“Android Activity側滑返回怎么實現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Android Activity側滑返回怎么實現(xiàn)”吧!
為趙縣等地區(qū)用戶提供了全套網頁設計制作服務,及趙縣網站建設行業(yè)解決方案。主營業(yè)務為做網站、成都網站制作、趙縣網站設計,以傳統(tǒng)方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!它的使用很簡單,首先要設置透明的窗口背景
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="android:textAllCaps">false</item> <item name="android:windowActionBar">false</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
然后
//setContent(View view)后 Slidr.attach(this);
步驟一 重新包裹界面
Slidr.class
public static SlidrInterface attach(final Activity activity, final int statusBarColor1, final int statusBarColor2){ //0 創(chuàng)建滑動嵌套界面SliderPanel final SliderPanel panel = initSliderPanel(activity, null); //7 Set the panel slide listener for when it becomes closed or opened // 監(jiān)聽回調 panel.setOnPanelSlideListener(new SliderPanel.OnPanelSlideListener() { ... //open close等 }); // Return the lock interface return initInterface(panel); } private static SliderPanel initSliderPanel(final Activity activity, final SlidrConfig config) { //3 獲取decorview ViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView(); //4 獲取我們布局的內容并刪除 View oldScreen = decorView.getChildAt(0); decorView.removeViewAt(0); //5 Setup the slider panel and attach it to the decor // 建立滑動嵌套視圖SliderPanel并且添加到DecorView中 SliderPanel panel = new SliderPanel(activity, oldScreen, config); panel.setId(R.id.slidable_panel); oldScreen.setId(R.id.slidable_content); //6 把我們的界面布局添加到SliderPanel,并且把SliderPanel添加到decorView中 panel.addView(oldScreen); decorView.addView(panel, 0); return panel; }
步驟二 使用ViewDragHelper.class
處理滑動手勢
SliderPanel.class
private void init(){ ... //1 ViewDragHelper創(chuàng)建 mDragHelper = ViewDragHelper.create(this, mConfig.getSensitivity(), callback); mDragHelper.setMinVelocity(minVel); mDragHelper.setEdgeTrackingEnabled(mEdgePosition); //2 Setup the dimmer view 添加用于指示滑動過程的View到底層 mDimView = new View(getContext()); mDimView.setBackgroundColor(mConfig.getScrimColor()); mDimView.setAlpha(mConfig.getScrimStartAlpha()); addView(mDimView); }
步驟三 在ViewDragHelper.Callback中處理我們的界面的拖動
我們首先明確ViewDragHelper僅僅是處理ParentView與它子View的關系,不會一直遍歷到最頂層的View。ViewDragHelper的捕獲capture是這樣實現(xiàn)的
@Nullable public View findTopChildUnder(int x, int y) { final int childCount = mParentView.getChildCount(); for (int i = childCount - 1; i >= 0; i--) { final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i)); if (x >= child.getLeft() && x < child.getRight() && y >= child.getTop() && y < child.getBottom()) { return child; } } return null; }
重點在SliderPanel.class的ViewDragHelper.Callback callback
的實現(xiàn),作者實現(xiàn)實現(xiàn)了很多個方向的滑動處理mLeftCallback、mRightCallback、mTopCallback、mBottomCallback、mVerticalCallback、mHorizontalCallback, 我們取mLeftCallback來分析
private ViewDragHelper.Callback mLeftCallback = new ViewDragHelper.Callback() { //捕獲View @Override public boolean tryCaptureView(View child, int pointerId) { boolean edgeCase = !mConfig.isEdgeOnly() || mDragHelper.isEdgeTouched(mEdgePosition, pointerId); //像前面說的,我們的內容是最上層子View,mDecorView這里指的是我們的contentView return child.getId() == mDecorView.getId() && edgeCase; } //拖動, 最終是通過view.offsetLeftAndRight(offset)實現(xiàn)移動 @Override public int clampViewPositionHorizontal(View child, int left, int dx) { return clamp(left, 0, mScreenWidth); } //滑動范圍 @Override public int getViewHorizontalDragRange(View child) { return mScreenWidth; } //釋放處理,判斷是滾回屏幕 @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); int left = releasedChild.getLeft(); int settleLeft = 0; int leftThreshold = (int) (getWidth() * mConfig.getDistanceThreshold()); boolean isVerticalSwiping = Math.abs(yvel) > mConfig.getVelocityThreshold(); if(xvel > 0){ if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){ settleLeft = mScreenWidth; }else if(left > leftThreshold){ settleLeft = mScreenWidth; } }else if(xvel == 0){ if(left > leftThreshold){ settleLeft = mScreenWidth; } } //滾動到left=0(正常布局) 或者 滾動到left=mScreenWidth(滾出屏幕)關閉Activity mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop()); invalidate(); } //轉換位置百分比,確定指示層的透明度 @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); float percent = 1f - ((float)left / (float)mScreenWidth); if(mListener != null) mListener.onSlideChange(percent); // Update the dimmer alpha applyScrim(percent); } //回調到Slidr處理Activity狀態(tài) @Override public void onViewDragStateChanged(int state) { super.onViewDragStateChanged(state); if(mListener != null) mListener.onStateChanged(state); switch (state){ case ViewDragHelper.STATE_IDLE: if(mDecorView.getLeft() == 0){ // State Open if(mListener != null) mListener.onOpened(); }else{ // State Closed 這里回調到Slidr處理activity.finish() if(mListener != null) mListener.onClosed(); } break; case ViewDragHelper.STATE_DRAGGING: break; case ViewDragHelper.STATE_SETTLING: break; } } };
對于mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop());
內部是使用Scroller.class輔助滾動,所以要在SliderPanel
中重寫View.computeScroll()
@Override public void computeScroll() { super.computeScroll(); if(mDragHelper.continueSettling(true)){ ViewCompat.postInvalidateOnAnimation(this); } }
到此,相信大家對“Android Activity側滑返回怎么實現(xiàn)”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
當前名稱:AndroidActivity側滑返回怎么實現(xiàn)-創(chuàng)新互聯(lián)
分享地址:http://aaarwkj.com/article6/copoig.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、商城網站、網站建設、App開發(fā)、網站營銷、全網營銷推廣
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容