Google官方在14年Google I/O上推出了全新的設(shè)計(jì)語言——Material Design碉咆。一并推出了一系列實(shí)現(xiàn)Material Design效果的控件庫——AndroidDesign Support Library。其中,有TabLayout, NavigationView,Floating labels for editing text,Floating Action Button,Snackbar, CoordinatorLayout, CollapsingToolbarLayout等等控件。在今后的學(xué)習(xí)中改化,我將一一介紹它們的特點(diǎn)和用法辨嗽。
我們可以使用三方開源的PagerSlidingTabStrip去實(shí)現(xiàn)猜扮,或者viewpagerindicator郎笆,我一般都偏向前者√饭#現(xiàn)在我們可以使用Design support library庫的TabLayout去實(shí)現(xiàn)了。
Demo地址:https://github.com/setasmallgoal/TabLayoutDemo
Demo首頁圖:
最終的效果圖:
一宛蚓、TabLayout基本使用 激捏。
1.在應(yīng)用的Build.gradle中添加support.design支持庫(注意:必須和v7包的版本相同)。
compile 'com.android.support:design:25.3.1'
2.TabLayout第一種使用–Tab固定模式凄吏。
1)創(chuàng)建布局文件activity_short_tab,在布局文件中添加TabLayout及ViewPager远舅。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.linwei.lw.tablayoutdemo.ui.activity.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
2)定義一個(gè)FragmentFactivity的Fragment工廠類,專門生產(chǎn)Fragment對(duì)象(用了工廠類痕钢,方便Fragment使用图柏,提高了性能,在項(xiàng)目中很常見任连,記得在android5.0的Demo中也是怎么寫的蚤吹,可能有些讀者覺得沒必要,也可以每次去創(chuàng)建Fragment随抠,并添加到ViewPager中也可)裁着。
package com.linwei.lw.tablayoutdemo.ui.fragment;
import java.util.HashMap;
/**
* Fragment工廠類。
* Created by LW on 2017/4/20.
*/
public class FragmentFactory {
private static HashMap<Integer, BaseFragment> mBaseFragments = new HashMap<Integer, BaseFragment>();
public static BaseFragment createFragment(int pos) {
BaseFragment baseFragment = mBaseFragments.get(pos);
if (baseFragment == null) {
switch (pos) {
case 0:
baseFragment = new TopLineFragment();//頭條
break;
case 1:
baseFragment = new NewsFragment();//要聞
break;
case 2:
baseFragment = new EntertainmentFragment();//娛樂
break;
case 3:
baseFragment = new SportsFragment();//體育
break;
case 4:
baseFragment = new FinanceFragment();//財(cái)經(jīng)
break;
case 5:
baseFragment = new ScienceFragment();//科技
break;
case 6:
baseFragment = new ModeFragment();//時(shí)尚
break;
case 7:
baseFragment = new VideoFragment();//視頻
break;
case 8:
baseFragment = new DirectSeedingFragment();//直播
break;
}
mBaseFragments.put(pos, baseFragment);
}
return baseFragment;
}
}
3)定義一個(gè)Fragment的父類BaseFragment拱她,我就不解釋了看代碼二驰。
package com.linwei.lw.tablayoutdemo.ui.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by LW on 2017/4/20.
*/
public abstract class BaseFragment extends Fragment {
protected Context mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContent = getContext();//上下文。
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return initView();//初始化布局秉沼。
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadData();//初始化數(shù)據(jù)桶雀。
}
protected abstract void loadData();
protected abstract View initView();
}
4)BaseFragment的實(shí)現(xiàn)子類TopLineFragment,這里我就只列出一個(gè)子類代碼唬复,其他子類代碼也是一樣的矗积。
package com.linwei.lw.tablayoutdemo.ui.fragment;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by LW on 2017/4/21.
*/
public class TopLineFragment extends BaseFragment {
@Override
protected void loadData() {
Toast.makeText(mContent,"Fragment頭條加載數(shù)據(jù)",Toast.LENGTH_SHORT).show();
}
@Override
protected View initView() {
TextView mView = new TextView(mContent);
mView.setText("Fragment頭條");
mView.setGravity(Gravity.CENTER);
mView.setTextSize(18);
mView.setTextColor(Color.BLACK);
return mView;
}
}
5)arrays.xml中第一顯示數(shù)據(jù)。我了方便使用盅抚,通過字符串?dāng)?shù)組定義起來漠魏。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="tab_short_Title">
<item>頭條</item>
<item>要聞</item>
<item>娛樂</item>
<item>體育</item>
</string-array>
<string-array name="tab_long_Title">
<item>頭條</item>
<item>要聞</item>
<item>娛樂</item>
<item>體育</item>
<item>財(cái)經(jīng)</item>
<item>科技</item>
<item>時(shí)尚</item>
<item>視頻</item>
<item>直播</item>
</string-array>
</resources>
6)ShortTabActivity類,實(shí)現(xiàn)TabLayout和ViewPager的業(yè)務(wù)邏輯妄均。
package com.linwei.lw.tablayoutdemo.ui.activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.linwei.lw.tablayoutdemo.R;
import com.linwei.lw.tablayoutdemo.Utils.CommentUtils;
import com.linwei.lw.tablayoutdemo.ui.fragment.BaseFragment;
import com.linwei.lw.tablayoutdemo.ui.fragment.FragmentFactory;
public class ShortTabActivity extends AppCompatActivity {
private TabLayout mTab;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_short_tab);
initView();
initData();
}
private void initData() {
ShortPagerAdapter adapter = new ShortPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mTab.setupWithViewPager(mViewPager);
}
private void initView() {
mTab = (TabLayout) findViewById(R.id.tab);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
}
private class ShortPagerAdapter extends FragmentPagerAdapter {
public String[] mTilte;
public ShortPagerAdapter(FragmentManager fm) {
super(fm);
mTilte = getResources().getStringArray(R.array.tab_short_Title);
}
@Override
public CharSequence getPageTitle(int position) {
return mTilte[position];
}
@Override
public BaseFragment getItem(int position) {
BaseFragment fragment = FragmentFactory.createFragment(position);
return fragment;
}
@Override
public int getCount() {
return CommentUtils.TAB_SHORT_COUNT;
}
}
}
TabLayout其他模式柱锹,我就不一一介紹,這里有Demo和CSDN詳細(xì)文章丰包。
原文出處:http://blog.csdn.net/persistence_lw/article/details/70473255
最后禁熏,附上源碼地址:https://github.com/setasmallgoal/TabLayoutDemo
(原文來自本人,謝謝閱讀)