Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果
首先來(lái)看一下效果:
大體思路如下:
總體布局用了一個(gè)自定義的ViewGroup,里面包了兩個(gè)View(top View,bottomView)
我在bottomView里放了ViewPager,里面又有Fragment,F(xiàn)ragment里放的是ListView
原理:
ViewGroup在分發(fā)touchEvent的時(shí)候先通過(guò)手勢(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)聽(tīng)重要的是打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è)量出來(lái)
2、onLayout時(shí)需要手動(dòng)將子控件布局
接下來(lái)就是監(jiān)聽(tīng)手勢(shì)設(shè)置動(dòng)畫(huà),不停的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();
}
項(xiàng)目地址在這:
總結(jié)
以上所述是小編給大家介紹的Android 根據(jù)手勢(shì)頂部View自動(dòng)展示與隱藏效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自定義View之漸變色折線圖的實(shí)現(xiàn)
折線圖的實(shí)現(xiàn)方法在github上有很多開(kāi)源的程序,但是對(duì)于初學(xué)者來(lái)講,簡(jiǎn)單一點(diǎn)的教程可能更容易入門(mén),下面這篇文章主要給大家介紹了關(guān)于Android自定義View之漸變色折線圖的相關(guān)資料,需要的朋友可以參考下2022-04-04
android短信管理器SmsManager實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了android短信管理器SmsManager實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android開(kāi)發(fā)之媒體播放工具類(lèi)完整示例
這篇文章主要介紹了Android開(kāi)發(fā)之媒體播放工具類(lèi),結(jié)合完整實(shí)例形式分析了基于MediaPlayer的事件監(jiān)聽(tīng)與多媒體文件播放相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
基于Android實(shí)現(xiàn)仿QQ5.0側(cè)滑
本課程將帶領(lǐng)大家通過(guò)自定義控件實(shí)現(xiàn)QQ5.0側(cè)滑菜單,課程將循序漸進(jìn),首先實(shí)現(xiàn)最普通的側(cè)滑菜單,然后引入屬性動(dòng)畫(huà)與拖動(dòng)菜單效果相結(jié)合,最終實(shí)現(xiàn)QQ5.0側(cè)滑菜單效果。通過(guò)本課程大家會(huì)對(duì)側(cè)滑菜單有更深層次的了解,通過(guò)自定義控件和屬性動(dòng)畫(huà)打造千變?nèi)f化的側(cè)滑菜單效果2015-12-12
Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單
這篇文章主要為大家詳細(xì)介紹了Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android開(kāi)發(fā)之資源目錄assets與res/raw的區(qū)別分析
這篇文章主要介紹了Android開(kāi)發(fā)之資源目錄assets與res/raw的區(qū)別,結(jié)合實(shí)例形式分析了Android開(kāi)發(fā)中資源目錄assets與res/raw的具體功能、使用方法與區(qū)別,需要的朋友可以參考下2016-01-01
解決android.support.v4.content.FileProvide找不到的問(wèn)題
這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

