Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例
1.背景
在項(xiàng)目開發(fā)過程中,有時(shí)候會(huì)碰到這樣的需求:在滑動(dòng)的過程中,在某時(shí)要將子view固定在頂部(常見的是將界面中的tab在滑動(dòng)到頂部的時(shí)候進(jìn)行固定)。
之前寫過一篇滑動(dòng)組件懸浮固定在頂部的文章,但感覺還是有些復(fù)雜,因此就有了這次的實(shí)現(xiàn)。效果圖:
2.思路
(CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout)+TabLayout+ViewPager
3.代碼實(shí)現(xiàn)
a.主布局代碼
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.ganshenml.slideholdsmoothdemo.ScrollingActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="30dp" app:popupTheme="@style/AppTheme.PopupOverlay"></android.support.v7.widget.Toolbar> <ImageView android:layout_width="match_parent" android:layout_height="280dp" android:scaleType="centerCrop" android:src="@drawable/bg" /> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="@color/colorAccent"></android.support.design.widget.TabLayout> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_scrolling" /> </android.support.design.widget.CoordinatorLayout>
需要注意的是:
- app:layout_scrollFlags="scroll|exitUntilCollapsed"——>設(shè)置可以滑動(dòng)且當(dāng)前view可以一直退出直到折疊視圖顯現(xiàn)。
- <include layout="@layout/content_scrolling" />——>引用的子view布局其實(shí)就是一個(gè)ViewPager(需要注意的是要在布局中設(shè)置:app:layout_behavior="@string/appbar_scrolling_view_behavior")
b.主界面Activity代碼
public class ScrollingActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scrolling); initViews(); } private void initViews() { tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setOffscreenPageLimit(2); viewPager.setAdapter(new MPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); } }
c.適配器MPagerAdapter代碼
public class MPagerAdapter extends FragmentStatePagerAdapter { private String[] tabTitle = new String[]{"tab01", "tab02"}; private FirstFragment firstFragment; private SecondFragment secondFragment; public MPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (position == 0) { if (firstFragment == null) { firstFragment = new FirstFragment(); } return firstFragment; } else if (position == 1) { if (secondFragment == null) { secondFragment = new SecondFragment(); } return secondFragment; } return null; } @Override public int getCount() { return tabTitle.length; } @Override public CharSequence getPageTitle(int position) { return tabTitle[position]; } }
兩個(gè)Fragment的代碼非常簡(jiǎn)單。僅僅加載布局而已,所以在此就不貼出來了。
4.擴(kuò)展
a.關(guān)于CollapsingToolbarLayout中子view的排列順序?qū)︼@示結(jié)果造成的影響
如圖:
可以看到圖中黑色邊框顯示的內(nèi)容不一致,因此ToolBar和ImageView的排列順序會(huì)對(duì)視圖的顯示結(jié)果造成影響。
推測(cè)——>CollapsingToolbarLayout中以上三種view不同排序的剖面展示效果為:
順序:Toolbar——>ImageView——>TabLayout(設(shè)置layout_gravity="bottom")
順序:ImageView——>Toolbar——>TabLayout(設(shè)置layout_gravity="bottom")
不負(fù)責(zé)任滴猜測(cè):把Toolbar看做一張畫布,只有覆蓋在畫布投射區(qū)域范圍內(nèi)的內(nèi)容才顯示出來在該畫布內(nèi)。
(因此,1.在畫布下的內(nèi)容就無法顯示出來;2.無法覆蓋畫布的內(nèi)容就顯示為畫布默認(rèn)的樣式)
所以,如果不想要有視差效果的話,那么就將Toolbar與TabLayout的高度設(shè)置一致。如果將Toolbar去掉,那么所有的CollapsingToolbarLayout中的View都會(huì)滑出界面,此時(shí)布局就變成了普通布局了(相當(dāng)于CollapsingToolbarLayout變成了CollapsingLayout)。
b.去掉Toolbar實(shí)現(xiàn)固定效果
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <ImageView android:layout_width="match_parent" android:layout_height="280dp" android:scaleType="centerCrop" android:src="@drawable/bg" /> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="top" android:background="@color/colorAccent"></android.support.design.widget.TabLayout> </android.support.design.widget.AppBarLayout>
只要將TabLayout從CollapsingToolbarLayout中移到AppBarLayout的一級(jí)子View即可。
(這樣也避免了:在CollapsingToolbarLayout中,因?yàn)橐晥D折疊覆蓋的問題,會(huì)導(dǎo)致整個(gè)ImageView被TabLayout覆蓋一部分而顯示不完全的問題。)
查看完整代碼,點(diǎn)擊:GitHub地址>>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果
這篇文章主要為大家詳細(xì)介紹了AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08超精準(zhǔn)的Android手機(jī)計(jì)步器開發(fā)
這篇文章主要為大家詳細(xì)介紹了超精準(zhǔn)的Android手機(jī)計(jì)步器開發(fā)過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android UI設(shè)計(jì)與開發(fā)之ViewPager介紹和簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之ViewPager介紹和簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
這篇文章主要介紹了Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存的相關(guān)資料,希望通過本文能幫助到大家,徹底會(huì)使用Volley,需要的朋友可以參考下2017-09-09Android應(yīng)用中使用TabHost組件繼承TabActivity的布局方法
這篇文章主要介紹了Android應(yīng)用中使用TabHost組件繼承TabActivity的布局方法,文中分別介紹了以Activity和以布局文件進(jìn)行布局的方式,需要的朋友可以參考下2016-04-04Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼
這篇文章主要介紹了Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android 調(diào)用系統(tǒng)照相機(jī)拍照和錄像
本文主要介紹Android 調(diào)用系統(tǒng)照相機(jī)拍照和錄像的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下2016-09-09Android 實(shí)現(xiàn)截屏功能的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)截屏功能的實(shí)例的相關(guān)資料,這里實(shí)現(xiàn)截屏的實(shí)例在代碼中注釋非常清楚,希望能幫助到大家,需要的朋友可以參考下2017-08-08