XMLNS: XMLNS: :layout - Width :orientation :layout - Height XMLNS: :context
XMLNS: XMLNS: :layout - Width :orientation :layout - Height XMLNS: :context
com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.auto.autohome1.TabFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="scrollable"
android:background="#9f3036"
app:tabIndicatorColor="#ffffff"
app:tabSelectedTextColor="#ae6061"
app:tabTextColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</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.support.v4.view.ViewPager>
</LinearLayout>
package com.auto.autohome1;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_tab,container,false);
tabLayout=(TabLayout)v.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager=(ViewPager)v.findViewById(R.id.viewpager);
viewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
//nothing here
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
//nothing here
}
});
//set an adpater
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return v;
}