CoordinatorLayout+AppBarLayout的使用

AppBarLayout主要實現上劃下滑的時候隱藏控件並讓一個控件懸浮,一些酷炫的寫法見博客:http://blog.csdn.net/huachao1001/article/details/51558835
這是我在網上看見別人寫的,我這裏主要記錄一下簡單的應用。是一個圖片+TabLaout,上劃的時候隱藏圖片,下拉的時候顯示

代碼如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:background="@mipmap/dog"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:layout_scrollFlags="scroll"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="#f00"
            app:tabSelectedTextColor="#f00"
            app:tabTextColor="#000" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
</android.support.design.widget.CoordinatorLayout>

下面是java代碼的一部分:


 //tabLayout標題
    private TabLayout tablayout;
    //tabLayout的內容
    private String[] titles={"仿listview","仿gridview","瀑布流"}

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.init(); //控件初始化
        this.setDataToTabLayout();//向tablayout填充數據
        this.setFragmentToViewpager();    //轉填fragment
        this.BindViewpager();//tablayout綁定viewpager


    }
    //控件初始化
    private void init(){
        viewPager= (ViewPager) findViewById(R.id.viewpager);
        tablayout= (TabLayout) findViewById(R.id.tablayout);
        appbar= (AppBarLayout) findViewById(R.id.appbar);
    }

    //向tablayout填充數據
    private void setDataToTabLayout(){
        Log.e("asd",tablayout+"");
        Log.e("asd",titles+"");
        for (int i = 0; i < titles.length; i++) {
            // tablayout.addTab(tablayout.newTab().setText(titles[i]));
        }
    }

    //轉填fragment
    private void setFragmentToViewpager(){
       //略。。。。
        MyFragmentAdapter adapter=new MyFragmentAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);
    }

    //tablayout綁定viewpager
    private void BindViewpager(){
        /**
         * 執行setupWithViewPager後,tablayout還在,
         * 但是數據會清空,所以要重新賦值
         */
        tablayout.setupWithViewPager(viewPager);

        for (int i = 0; i < titles.length; i++) {
            tablayout.getTabAt(i).setText(titles[i]);
        }
    }

注意:1、
scroll 所有想要滾動出屏幕的view都需要設置flags 沒有設置flags的view都會固定在屏幕的頂部
例如:TabLayout沒有設置該屬性 滾動時會停留在屏幕頂部

  enterAlways 設置該falgs時 每次向下滾動該view變爲可見,如果不設置,只有當下面的滾動佈局,比如AppBarLayout下面是個listview,那麼不設置這個flag,當listview向上滑的時候控件隱藏,只有當listview滑到頭部的時候在向下滑控件纔會出現,如果設置了此屬性,每次listview向下滑的時候控件都會出現

注意2、TabLaout綁定viewpager的時候會清空裏面的數據,需要
tablayout.getTabAt(0).setText(titles[0]);這樣重新賦值

發佈了50 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章