這篇文章主要介紹Android如何根據(jù)手勢(shì)實(shí)現(xiàn)頂部View自動(dòng)展示與隱藏效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)紅旗免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。首先來看一下效果:
大體思路如下:
總體布局用了一個(gè)自定義的ViewGroup,里面包了兩個(gè)View(top View,bottomView)
我在bottomView里放了ViewPager,里面又有Fragment,F(xiàn)ragment里放的是ListView
原理:
ViewGroup在分發(fā)touchEvent的時(shí)候先通過手勢(shì)GestureDetector判斷手勢(shì)方向,當(dāng)向上滑動(dòng)的時(shí)候讓topView和bottomView同時(shí)向上移動(dòng),反之亦然。
整體思路不是很難如下是干貨:
布局文件
<com.lin.gesturedetector.MyViewGroup android:id="@+id/view_group" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/group_top" layout="@layout/view_top" /> <include android:id="@+id/group_bottom" layout="@layout/view_bottom" /> </com.lin.gesturedetector.MyViewGroup>
手勢(shì)監(jiān)聽重要的是打log看一下上下滑動(dòng)是數(shù)值的變化,找到其規(guī)律:
@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i(tag, "onScroll -> distanceY" + distanceY); if (distanceY < 0) {// 手勢(shì)向下滑動(dòng)是負(fù)值 animatorLayoutOffset(1); } if (distanceY > 0) { animatorLayoutOffset(0f); } return true; }
一定記得在ViewGroup內(nèi)查找控件需要在onFinishInflate后才能找到:
@Override protected void onFinishInflate() { super.onFinishInflate(); viewTop = findViewById(R.id.group_top); viewBottom = findViewById(R.id.group_bottom); }
在ViewGroup布局的邏輯中需要處理的有一下幾點(diǎn):
1、onMeasure的時(shí)候要把子控件測(cè)量出來
2、onLayout時(shí)需要手動(dòng)將子控件布局
接下來就是監(jiān)聽手勢(shì)設(shè)置動(dòng)畫,不停的onLayout以達(dá)到topView和bottomView的布局效果
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); viewTop.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)); viewBottom.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); setMeasuredDimension(width, height); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int topHeight = viewTop.getMeasuredHeight(); float offset = layoutOffset * topHeight; int width = r - l; float topViewYTop = offset - topHeight; float topViewYBottom = topViewYTop + topHeight; viewTop.layout(0, (int) topViewYTop, width, (int) topViewYBottom); viewBottom.layout(0, (int) topViewYBottom, width, (int) topViewYBottom + viewBottom.getMeasuredHeight()); } private void animatorLayoutOffset(float offset) { if (animator != null && animator.isRunning()) { return; } animator = ObjectAnimator.ofFloat(this, "layoutOffset", layoutOffset, offset); animator.setDuration(500); animator.start(); }
以上是“Android如何根據(jù)手勢(shì)實(shí)現(xiàn)頂部View自動(dòng)展示與隱藏效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁名稱:Android如何根據(jù)手勢(shì)實(shí)現(xiàn)頂部View自動(dòng)展示與隱藏效果-創(chuàng)新互聯(lián)
文章URL:http://newbst.com/article38/dippsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、ChatGPT、手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容