Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个support库里面,Google给我们提供了更加规范的MD设计风格的控件。最重要的是,Android Design Support Library的兼容性更广,直接可以向下兼容到Android 2.2。
app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.hd.tabviewtest"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:support-annotations:23.2.0'
compile 'com.android.support:design:23.2.0'
}
需要添加compile ‘com.android.support:design:23.2.0’包。
layout/activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hd.tabviewtest.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ADBE107E"
app:tabMode="scrollable"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
></android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
这里需要注意控件所属的包别用错了,android.widget.TabLayout也有一个TabLayout,我们需要使用design.support包中的控件。
com.hd.tabviewtest.MainActivity
package com.hd.tabviewtest;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout mTablayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
initViewger();
}
private void initViewger() {
mTablayout = (android.support.design.widget.TabLayout) findViewById(R.id.tabs);
List<String> titles = new ArrayList<>();
titles.add("精选");
titles.add("体育");
titles.add("巴萨");
titles.add("明星");
titles.add("视频");
titles.add("健康");
titles.add("励志");
titles.add("图文");
titles.add("本地");
titles.add("动漫");
titles.add("搞笑");
titles.add("精选");
for (int i=0;i<titles.size();i++) {
mTablayout.addTab(mTablayout.newTab().setText(titles.get(i)));
}
List<Fragment> fragmentList = new ArrayList<>();
for (int i =0;i<titles.size();i++) {
ListFragment fragment = new ListFragment();
Bundle bundle = new Bundle();
bundle.putString("title",titles.get(i));
fragment.setArguments(bundle);
fragmentList.add(fragment);
}
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
viewPager.setAdapter(adapter);
mTablayout.setupWithViewPager(viewPager);
}
}
这里主要说下mTablayout.setupWithViewPager(viewPager)这个方法。这个方法会把TabLayout和ViewPager进行关联,无论哪一个控件改变,另外一个控件都会对应做出改变。当然,这是因为适配器的功能,当进行页面切换时,viewpger会回调适配器中的getItem方法去加载对应Frament,而TabLayout也会回调适配器中的getPageTitle方法。
layout/fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="fragment1"/>
</LinearLayout>
package com.hd.tabviewtest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/** * Created by hd on 2016/3/5. */
public class ListFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
String title = getArguments().getString("title");
View view = inflater.inflate(R.layout.fragment_1, container, false);
TextView mTextView = (TextView) view.findViewById(R.id.textView);
mTextView.setText(title);
return view;
}
}
com.hd.tabviewtest.FragmentAdapter
package com.hd.tabviewtest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;
/** * Created by hd on 2016/3/5. */
public class FragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList;
private List<String> mTitleList;
public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentsList, List<String> TitleList) {
super(fm);
mFragmentList = fragmentsList;
mTitleList = TitleList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}
}