【FastDev4Android框架開發】Android Design支持庫TabLayout打造仿網易新聞Tab標籤效果(三十七)

轉載請標明出處:

http://blog.csdn.net/developer_jiangqq/article/details/50158985

本文出自:【江清清的博客】


().前言:   

           【好消息】個人網站已經上線運行,後面博客以及技術乾貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org

        仿36Kr客戶端開發過程中,因爲他們網站上面的新聞文章分類比較多,所以我這邊還是打算模仿網易新聞APP的主界面新聞標籤Tab以及頁面滑動效果來進行實現。要實現的頂部的Tab標籤的效果有很多方法例如採用開源項目ViewPagerIndicator中的TabPageIndicator就可以實現。不過我們今天不講ViewPagerIndicator,我們來講一下Google今年發佈的TabLayout組件。在2015年的Google大會上,google發佈的新的Android Support Design庫,裏面也包含了幾個新的控件,那麼TabLayout就是其中一個。使用該組件我們可以很輕鬆的實現TabPageIndicator效果,並且該爲官方的,可以向下兼容很多版本而且可以更加統一Material Design效果。不過查看TabLayout的源碼發現該組件也是繼承自HorizontalScrollView實現。

          使用HorizontalScrollView打造仿網易tab標籤效果,點擊進入...

          本例子具體代碼已經上傳到下面的項目中,歡迎各位去star和fork一下。

         FastDev4Android框架項目地址:https://github.com/jiangqqlmj/FastDev4Android

().使用方式:   

        2.1.我這邊的項目採用Android Studio進行開發的,所以首先要引入TabLayout的依賴庫,如下:

compile'com.android.support:design:23.1.1'
compile'com.android.support:appcompat-v7:23.1.1'


().效果實現:  

          我們現在需要仿照網易新聞(36Kr客戶端)Tab標籤切換以及底部新聞頁面切換的效果。Tab標籤實現採用TabLayout組件,底部頁面切換採用Fragment+ViewPager+FragmentStatePagerAdapter實現。

          []:承載FragmentActivity這邊就不講解了,具體可以看上一篇文章和本項目的代碼即可。那我們直接從Fragment開始講起。

          3.1.首先我們需要給Fragment創建一個佈局文件如下:

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"android:layout_width="match_parent"
   android:layout_height="match_parent">
    <!--頂部tab標籤容器-->
    <android.support.design.widget.TabLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
        android:id="@+id/tab_layout"
       android:background="@color/white"
       app:tabIndicatorColor="@color/color_selected"
       app:tabSelectedTextColor="@color/color_selected"
       app:tabTextColor="@color/color_unselected"
 
       ></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
       android:id="@+id/info_viewpager"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        />
</LinearLayout>

        該佈局主要爲兩個控件:第一部分爲TableLayout組件該爲Tab標籤的容器,第二部分爲ViewPager組件主要用於顯示若干個Fragment進行頁面切換。大家在TabLayout組件中應該已經注意到了三個自定義屬性如下:

app:tabIndicatorColor="@color/white"                 // 下方滾動的下劃線顏色 
app:tabSelectedTextColor="@color/gray"               // tab被選中後,文字的顏色 
app:tabTextColor="@color/white"                      // tab默認的文字顏色 

這三個自定義屬性是TabLayout提供的,我們可以任意修改標題顏色以及指示器的顏色,除此之外還提供了以下一些風格設置方法:

  • tabMaxWidth
  • tabIndicatorColor
  • tabIndicatorHeight
  • tabPaddingStart
  • tabPaddingEnd
  • tabBackground
  • tabTextAppearance
  • tabSelectedTextColor

      3.2.接着是是CNKFixedPagerAdapter,該爲ViewPager的自定義適配器,在ViewPager中的每一項採用Fragment實現,所以傳入了得Fragment的頁面的集合,同時還定義了Tab顯示標題的數組。

package com.chinaztt.fda.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.chinaztt.fda.application.FDApplication;
import com.chinaztt.fda.ui.R;
 
import java.util.List;
/**
 * 當前類註釋:Fragment,Viewpager的自定義適配器
 * 項目名:FastDev4Android
 * 包名:com.chinaztt.fda.adapter
 * 作者:江清清 on 15/12/2 10:08
 * 郵箱:[email protected]
 * QQ: 781931404
 * 公司:江蘇中天科技軟件技術有限公司
 */
public class CNKFixedPagerAdapter extends FragmentStatePagerAdapter {
    private String[] titles;
    private LayoutInflater mInflater;
    public void setTitles(String[] titles) {
        this.titles = titles;
    }
    private List<Fragment> fragments;
    public CNKFixedPagerAdapter(FragmentManager fm) {
        super(fm);
    }
 
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }
    @Override
    public int getCount() {
        return this.fragments.size();
    }
 
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment=null;
        try {
           fragment=(Fragment)super.instantiateItem(container,position);
        }catch (Exception e){
 
        }
        return fragment;
    }
 
    @Override
    public void destroyItem(ViewGroupcontainer, int position, Object object) {
 
    }
    //此方法用來顯示tab上的名字
    @Override
    public CharSequence getPageTitle(intposition) {
 
        return titles[position %titles.length];
    }
    public List<Fragment> getFragments(){
        return fragments;
    }
    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }
}

Adapter中我們重寫了getPageTitle()方法,用來顯示Tab的標題。

   3.3.下面就是具體的Fragment(TabInfoFragment)了,該Fragment中,我們初始化ViewPager,TabLayout以及自定義器適配器,Fragment頁面和顯示的Tab標題。

最終我們把適配器和ViewPager進行綁定,TabLayout和ViewPager進行綁定即可。

public class TabInfoFragment extends Fragment {
    private String[]titles=new String[]{"全部","氪TV","O2O","新硬件","Fun!!","企業服務","Fit&Health","在線教育","互聯網金融","大公司","專欄","新產品"};
    private View mView;
    private TabLayout tab_layout;
    private ViewPager info_viewpager;
    private List<Fragment> fragments;
    private CNKFixedPagerAdapter mPagerAdater;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(mView==null){
            mView=inflater.inflate(R.layout.tab_info_fragment_layout,container,false);
            initViews();
            initValidata();
        }
        return mView;
    }
    private void initViews(){
       tab_layout=(TabLayout)mView.findViewById(R.id.tab_layout);
       info_viewpager=(ViewPager)mView.findViewById(R.id.info_viewpager);
 
    }
    private void initValidata(){
        fragments=new ArrayList<>();
        for(int i=0;i<12;i++){
            OneFragment oneFragment=new OneFragment();
            Bundle bundle=new Bundle();
           bundle.putString("extra",titles[i]);
            oneFragment.setArguments(bundle);
            fragments.add(oneFragment);
        }
        //創建Fragment的 ViewPager 自定義適配器
        mPagerAdater=new CNKFixedPagerAdapter(getChildFragmentManager());
        //設置顯示的標題
        mPagerAdater.setTitles(titles);
        //設置需要進行滑動的頁面Fragment
        mPagerAdater.setFragments(fragments);
 
       info_viewpager.setAdapter(mPagerAdater);
       tab_layout.setupWithViewPager(info_viewpager);
 
 
        //設置Tablayout
        //設置TabLayout模式 -該使用Tab數量比較多的情況
       tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}

    相信大家已經看到該代碼的最後有一句:

 tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);

 該用來設置Tablayout的模式,除了上面的默認之外還有一個模式如下:

 tab_layout.setTabMode(TabLayout.MODE_FIXED);

 兩者的區別如下,如果Tab數量比較多的情況下,最少用上面那個,Tab標籤可以左右滑動顯示。

    3.4.運行效果如下:


().Tab升級版改造:  

         上面的效果是屬於基礎版本直接Tab顯示標題就結束了,有時候不會達到我們的要求Tab標籤上面顯示圖標。那麼現在我們對其進行改造一下可以讓TabLayout打造的Tab既能顯示圖標又能顯示文字標題信息。該主要採用Tab的以下方法實現:

tab.setCustomView(view); tab添加的自定義佈局

        4.1.首先對於Tab Item每一項我們需要定義一個佈局文件:

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <ImageView
       android:src="@mipmap/ic_launcher"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:id="@+id/imageView"
       android:layout_gravity="center_horizontal"
        />
    <TextView
        android:text="Item 01"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:id="@+id/textView"
       android:layout_gravity="center_horizontal"
        />
</LinearLayout>

該佈局文件很簡單,上下佈局圖標和標題

4.2.然後我們需要在CNKFixedPagerAdapter中進行以下修改:先前我們是通過重寫public CharSequence getPageTitle(int position)該方法來顯示標題的,那麼現在我們需要刪掉或者註釋掉該方法,然後新增一個getTabView()方法來實時創建Tab Item的佈局然後綁定數據:

/**
     * 添加getTabView的方法,來進行自定義Tab的佈局View
     * @param position
     * @return
     */
    public View getTabView(int position){
       mInflater=LayoutInflater.from(FDApplication.getInstance());
        Viewview=mInflater.inflate(R.layout.tab_item_layout,null);
        TextView tv= (TextView)view.findViewById(R.id.textView);
        tv.setText(titles[position]);
        ImageView img = (ImageView)view.findViewById(R.id.imageView);
       img.setImageResource(R.mipmap.ic_launcher);
        return view;
    }

4.3.這些步驟做完了之後,在主Fragment(TabInfoFragment)中對Tablayout中每一項Tab作如下設置即可:獲取到每一項Tab,然後給該Tab設置自定義佈局調用tab.setCustomView()方法。

 //設置自定義Tab--加入圖標的demo
        for(int i=0;i<12;i++){
            TabLayout.Tab tab =tab_layout.getTabAt(i);
           tab.setCustomView(mPagerAdater.getTabView(i));
        }

 4.4.運行結果如下:


().最後總結

           今天我們通過Android  Design支持庫TabLayout組件實現了仿照網易新聞客戶端首頁的頁面滑動和頂部Tab效果。希望對大家有所幫助使用以前的方式雖然也可以顯示這樣的效果,不過技術在發展嘛,緊跟時代潮流吧~嘿嘿。

           本次實例代碼因爲比較多,重點核心代碼已經貼出來。不過實例註釋過的全部代碼已經上傳到Github項目中了。同時歡迎大家去Github站點進行clone或者fork瀏覽整個開源快速開發框架項目~

https://github.com/jiangqqlmj/FastDev4Android

 

尊重原創,轉載請註明:From Sky丶清(http://blog.csdn.net/developer_jiangqq) 侵權必究!

關注我的訂閱號,每天分享移動開發技術(Android/IOS),項目管理以及博客文章!

關注我的微博,可以獲得更多精彩內容

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章