转载自:https://blog.csdn.net/qq_16666847/article/details/53291941
我只设置了两个fragment显示,简单明了。
一. 导入依赖
compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.android.support:design:24.2.0'
二. xml布局
activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="50dp" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
fragment.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:text="fragment1" android:background="#c1c1c1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
三. fragment
两个Fragment 格式一样,这里我就放一个吧
package com.jmg.viewpagerfragment; 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; public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout1, container, false); } }
四. 设置fragment工厂
package com.jmg.viewpagerfragment; import android.support.v4.app.Fragment; public class FragmentFactory { public static Fragment getFragment(int position) { Fragment fragment = null; switch (position) { case 0: fragment = new Fragment1(); break; case 1: fragment = new Fragment2(); break; } return fragment; } }
五. MainActivity.java实现
package com.jmg.viewpagerfragment; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends AppCompatActivity { TabLayout tab; ViewPager viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tab = (TabLayout) findViewById(R.id.tabLayout); viewpager = (ViewPager) findViewById(R.id.viewPager); viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab)); tab.setupWithViewPager(viewpager); //tab和viewPager绑定 setAdapter(); } private void setAdapter() { viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return FragmentFactory.getFragment(position); } @Override public int getCount() { return 2; //tab的个数 } @Override public CharSequence getPageTitle(int position) { String[] str = new String[]{"首页", "应用"}; //tab显示的文字,数量要相同 return str[position]; } }); } }每天进步一点点!