In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to achieve Android Activity sideslip return". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve Android Activity sideslip return"!
Example of using Slidr
It is easy to use, first of all, to set a transparent window background.
False true @ color/colorPrimary @ color/colorPrimaryDark @ color/colorAccent true @ android:color/transparent
And then
/ / Slidr.attach (this) after setContent (View view)
Here are three steps to see how it works.
Step 1 re-wrap the interface
Slidr.class
Public static SlidrInterface attach (final Activity activity, final int statusBarColor1, final int statusBarColor2) {/ / 0 create a sliding nested interface SliderPanel final SliderPanel panel = initSliderPanel (activity, null); / / 7 Set the panel slide listener for when it becomes closed or opened / / listen for callback panel.setOnPanelSlideListener (new SliderPanel.OnPanelSlideListener () {... / open close, etc.}) / / Return the lock interface return initInterface (panel);} private static SliderPanel initSliderPanel (final Activity activity, final SlidrConfig config) {/ / 3 get decorview ViewGroup decorView = (ViewGroup) activity.getWindow () .getDecorView (); / / 4 get the content of our layout and delete View oldScreen = decorView.getChildAt (0) DecorView.removeViewAt (0); / / 5 Setup the slider panel and attach it to the decor / / create a sliding nested view SliderPanel and add it to DecorView SliderPanel panel = new SliderPanel (activity, oldScreen, config); panel.setId (R.id.slidable_panel); oldScreen.setId (R.id.slidable_content) / / 6 add our interface layout to SliderPanel and add SliderPanel to decorView panel.addView (oldScreen); decorView.addView (panel, 0); return panel;}
Step 2 use ViewDragHelper.class to process sliding gestures
SliderPanel.class
Private void init () {/ / 1 ViewDragHelper create mDragHelper = ViewDragHelper.create (this, mConfig.getSensitivity (), callback); mDragHelper.setMinVelocity (minVel); mDragHelper.setEdgeTrackingEnabled (mEdgePosition); / / 2 Setup the dimmer view add View to the underlying mDimView = new View (getContext ()); mDimView.setBackgroundColor (mConfig.getScrimColor ()); mDimView.setAlpha (mConfig.getScrimStartAlpha ()); addView (mDimView);}
Step 3 deal with the drag of our interface in ViewDragHelper.Callback
Let's first make it clear that ViewDragHelper only deals with the relationship between ParentView and its child View and does not traverse all the way to the top-level View. The capture capture of ViewDragHelper is implemented in this way
@ Nullable public View findTopChildUnder (int x, int y) {final int childCount = mParentView.getChildCount (); for (int I = childCount-1; I > = 0; iMurray -) {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的实现,作者实现实现了很多个方向的滑动处理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)实现移动 @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 }} / / scroll to left=0 (normal layout) or scroll to left=mScreenWidth (roll out of the screen) to turn off Activity mDragHelper.settleCapturedViewAt (settleLeft, releasedChild.getTop ()); invalidate () } / / percentage of conversion position to determine the transparency of the indicator layer @ 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) } / / callback to Slidr to process Activity status @ 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 callback to Slidr to process activity.finish () if (mListener! = null) mListener.onClosed ();} break; case ViewDragHelper.STATE_DRAGGING: break; case ViewDragHelper.STATE_SETTLING: break }}}
For mDragHelper.settleCapturedViewAt (settleLeft, releasedChild.getTop ()); Scroller.class is used internally to assist scrolling, so override View.computeScroll () in SliderPanel
@ Overridepublic void computeScroll () {super.computeScroll (); if (mDragHelper.continueSettling (true)) {ViewCompat.postInvalidateOnAnimation (this);}} so far, I believe you have a better understanding of "how to achieve Android Activity sideslip return". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.