Tablayout 學(xué)習(xí)
轉(zhuǎn)自
首先來看下實(shí)現(xiàn)的效果吧:
這里寫圖片描述
TabLayout簡介
- 在2015年的Google I/O大會(huì)上秧了,Google發(fā)布的新的Android Support Design庫,里面也包含了幾個(gè)新的控件鞋真,那么TabLayout就是其中一個(gè)乘盼。使用該組件我們可以很輕松的實(shí)現(xiàn)TabPageIndicator效果升熊,并且該為官方的,可以向下兼容很多版本而且可以更加統(tǒng)一Material Design效果绸栅。
使用的時(shí)候要在庫依賴中加入
如果不太清楚级野,可以看下http://blog.csdn.net/wuyinlei/article/details/50570018
教你怎么在項(xiàng)目中加入依賴。
compile'com.android.support:design:23.1.1’
接下來粹胯,在TabLayout中加入
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!-- 下方滾動(dòng)的下劃線顏色 -->
app:tabIndicatorColor="#33aa22"
<!-- 下方指示條的高度 -->
app:tabIndicatorHeight="5dp"
<!-- tab被選中后蓖柔,文字的顏色 -->
app:tabSelectedTextColor="#33aa22"
<!--可以改變tab中的字體的大小-->
app:tabTextAppearance="@style/App_Theme"
<!-- tab中字體的顏色 -->
app:tabTextColor="#33aa22"/>
可以在這里設(shè)置字體大小,然后引用:
<style name="App_Theme" parent="Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
<item name="android:textSize">18dp</item>
<item name="android:textAllCaps">false</item>
</style>
注意:上面的這個(gè)style必須在承載他的activity中theme中加入风纠,要不然會(huì)出現(xiàn)
Cause by :java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library;(我沒有加上之前各種錯(cuò)况鸣,加上正常運(yùn)行,但是運(yùn)行之后去掉竹观,竟然也可以正常運(yùn)行镐捧,不得解)
好了潜索,接下來我們看下具體怎么實(shí)現(xiàn)的吧,里面有解釋:
MainActivity.java:
package com.example.tablayoutfragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tablayoutfragment.MainActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--在這里直接添加fragment懂酱。并且制定fragment類名-->
<fragment
android:id="@+id/main_info_fragment"
class="com.example.tablayoutfragment.MainFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
MAinFragment.java:
package com.example.tablayoutfragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class MainFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager mViewPager;
//fragment存儲器
private List<Fragment> mFragments;
//tab條目中的內(nèi)容
private String[]titles = {"第一個(gè)","第二個(gè)","第三個(gè)","第四個(gè)","第五個(gè)","第六個(gè)","第七個(gè)","第八個(gè)"};
private FixedPagerAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
initData();
return view;
}
/**
* 初始化數(shù)據(jù)
*/
private void initData() {
mAdapter = new FixedPagerAdapter(getChildFragmentManager());
mAdapter.setTitles(titles);//標(biāo)題
mFragments = new ArrayList<>();
for (int i = 0; i < titles.length; i++) {
mFragments.add(PageFragment.newInstance(titles[i]));
}
//把要顯示的fragment集合傳給adapter
mAdapter.setFragments(mFragments);
/**
* 設(shè)置tablayout的模式
* 模式一:MODE_SCROLLABLE 可以滑動(dòng)竹习,顯示很多個(gè)tab中的幾個(gè)展示出來
* 模式二:MODE_FIXED Fixed tabs display all tabs concurrently 展示出所有的,適合三四個(gè)tab
*/
mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//給viewPager設(shè)置適配器
mViewPager.setAdapter(mAdapter);
//TabLayout綁定ViewPager
mTabLayout.setupWithViewPager(mViewPager);
}
}
fragment_main.xml:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
tools:context=".MainFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#33aa22"
app:tabIndicatorHeight="5dp"
app:tabSelectedTextColor="#33aa22"
app:tabTextAppearance="@style/App_Theme"
app:tabTextColor="#33aa22"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
</android.support.v4.view.ViewPager>
</LinearLayout>
PageFragment.java:
package com.example.tablayoutfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by 若蘭 on 2016/1/23.
* 一個(gè)懂得了編程樂趣的小白列牺,希望自己
* 能夠在這個(gè)道路上走的很遠(yuǎn)整陌,也希望自己學(xué)習(xí)到的
* 知識可以幫助更多的人,分享就是學(xué)習(xí)的一種樂趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/
public class PageFragment extends Fragment {
private View mView;
/**
* key值
*/
private static final String KEY = "EXTRA";
private String title;
/**
* 在這里我們提供一個(gè)靜態(tài)的方法來實(shí)例化PageFragment
* 在這里我們傳入一個(gè)參數(shù),用來得到title瞎领,然后我們拿到這個(gè)title設(shè)置給內(nèi)容
*
* @param extra
* @return
*/
public static PageFragment newInstance(String extra){
//利用bundle傳值
Bundle bundle = new Bundle();
bundle.putString(KEY,extra);
//實(shí)例化
PageFragment fragment = new PageFragment();
fragment.setArguments(bundle);
return fragment;
}
/* @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString(KEY);
}
}
*/
private TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString(KEY);
}
if (mView == null) {
mView = inflater.inflate(R.layout.page_fragment, container, false);
}
initView();
return mView;
}
public void initView() {
mTextView = (TextView) mView.findViewById(R.id.text_fragment);
mTextView.setText(title);
}
}
這個(gè)fragment的布局里面就一個(gè)textview泌辫,我就不寫上了。
下面看下這個(gè)adapter吧
FixedPagerAdapter.java:
package com.example.tablayoutfragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by 若蘭 on 2016/1/23.
* 一個(gè)懂得了編程樂趣的小白默刚,希望自己
* 能夠在這個(gè)道路上走的很遠(yuǎn)甥郑,也希望自己學(xué)習(xí)到的
* 知識可以幫助更多的人,分享就是學(xué)習(xí)的一種樂趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/
public class FixedPagerAdapter extends FragmentStatePagerAdapter {
private String[] titles;
/**
* 設(shè)置標(biāo)題
*
* @param titles
*/
public void setTitles(String[] titles) {
this.titles = titles;
}
private List<Fragment> mFragments;
/**
* 這個(gè)是在繼承FragmentStatePagerAdapter會(huì)強(qiáng)制寫入的
*
* @param fm
*/
public FixedPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
/**
* Return the number of views available.
* 返回一個(gè)可以用的view的個(gè)數(shù)
*
* @return
*/
@Override
public int getCount() {
return mFragments.size();
}
/**
* Create the page for the given position. The adapter is responsible for
* adding the view to the container given here,
* although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).
* 這個(gè)同destroyItem()相反逃魄,是對于給定的位置創(chuàng)建視圖荤西,適配器往container中添加
*
* @param container
* @param position
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = null;
fragment = (Fragment) super.instantiateItem(container,position);
return fragment;
}
public List<Fragment> getFragments() {
return mFragments;
}
public void setFragments(List<Fragment> fragments) {
mFragments = fragments;
}
/**
* Remove a page for the given position. The adapter is responsible for
* removing the view from its container,
* although it only must ensure this is done by the time it returns from finishUpdate(View).
* 移除給定位置的數(shù)據(jù),適配器負(fù)責(zé)從container(容器)中取出伍俘,但是這個(gè)必須保證是在finishUpdate(view)
* 返回的時(shí)間內(nèi)完成
*
* @param container
* @param position
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
/**
* 得到滑動(dòng)頁面的Title
*
* @param position
* @return
*/
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
好了邪锌,到此為止,運(yùn)行就可以出現(xiàn)了開頭的效果了“現(xiàn)在附上github該demo的地址:https://github.com/wuyinlei/TTFVF, 有疑問可以交流哈觅丰。多謝支持!妨退!