1.布局:
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.fragmentandviewpagerdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv1" android:onClick="onClick" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="漫画" android:textSize="30dp" android:textColor="#0040ff" /> <TextView android:id="@+id/tv2" android:onClick="onClick" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="新闻" android:textSize="30dp" /> <TextView android:id="@+id/tv3" android:onClick="onClick" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="百度" android:textSize="30dp" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </LinearLayout>2.在布局中写好对应的三个TextView展示的Fragment视图
这里只写一个了
fragment1
<?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"> <ListView android:id="@+id/list_view1" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
3.写好视图后可以详细的写每个视图的分布情况如;item1 item2 item3
4.在工程中定义好三个Fragment类 Fragment1 Fragment2 Fragment3
Fragment1
public class Fragment1 extends Fragment {@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); return view; } 5.适配器类public class Myadapter extends FragmentPagerAdapter { public Myadapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { //初始化一个空的fragment //当为0 1 2的时候,分别为对应的fragment1 fragment2 fragment3视图。 Fragment fragment=null; switch (position){ case 0: fragment=new Fragment1(); break; case 1: fragment=new Fragment2(); break; case 2: fragment=new Fragment3(); break; } return fragment; } @Override public int getCount() { return 3; } }
6.主界面public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ViewPager mvp; private TextView tv1,tv2,tv3; private Fragment fm1,fm2,fm3; private Myadapter md; private ArrayList<Fragment> mlist=new ArrayList<>(); private ArrayList<TextView> list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); } private void initview() { mvp=(ViewPager)findViewById(R.id.view_pager); fm1=new Fragment1();fm2=new Fragment2();fm3=new Fragment3(); mlist.add(fm1); mlist.add(fm2); mlist.add(fm3); //给viewpager设置适配器 FragmentManager fm = getSupportFragmentManager(); md=new Myadapter(fm); mvp.setAdapter(md); //找到TextView的ID,并装进集合 tv1=(TextView)findViewById(R.id.tv1); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); list.add(tv1);list.add(tv2);list.add(tv3); //ViewPager的监听事件 mvp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //当页面为选中页时设置TextView颜色为蓝,否则为黑 for (int i=0;i<list.size();i++){ if(i==position){ list.get(i).setTextColor(Color.BLUE); }else { list.get(i).setTextColor(Color.BLACK); } } } @Override public void onPageScrollStateChanged(int state) { } }); } //TextView的点击事件 当点击TextView时页面展示为对应的Fragment @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv1: mvp.setCurrentItem(0); break; case R.id.tv2: mvp.setCurrentItem(1); break; case R.id.tv3: mvp.setCurrentItem(2); break; } } }