0、目录结构
1、页面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_second" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_above="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/radioGroup" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="52dp"> <RadioButton android:id="@+id/btn1" android:button="@null" android:text="消息" android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" /> <RadioButton android:id="@+id/btn2" android:button="@null" android:text="联系人" android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" /> <RadioButton android:id="@+id/btn3" android:button="@null" android:text="动态" android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RadioGroup> </RelativeLayout>
2、activity代码
public class SecondActivity extends Activity implements RadioGroup.OnCheckedChangeListener{ private ViewPager viewPager; private ArrayList<View> pageViews; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); initFragment(); } private void initFragment() { MessageFragment messageFragment=new MessageFragment(); ContactsFragment contactsFragment=new ContactsFragment(); TrendsFragment trendsFragment=new TrendsFragment(); ArrayList<Fragment> fragments=new ArrayList<>(); fragments.add(messageFragment); fragments.add(contactsFragment); fragments.add(trendsFragment); viewPager.setAdapter(new mPagerAdapter()); //默认为消息页面 viewPager.setCurrentItem(0); } private void initView() { viewPager = (ViewPager) findViewById(R.id.vp); RadioGroup radioGroup= (RadioGroup) findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(this); //LayoutInflater获取View 与activity关联 LayoutInflater inflater=getLayoutInflater(); View message=inflater.inflate(R.layout.layout_message,null); View contacts=inflater.inflate(R.layout.layout_contacts,null); View trends=inflater.inflate(R.layout.layout_trends,null); pageViews = new ArrayList<>(); pageViews.add(message); pageViews.add(contacts); pageViews.add(trends); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.btn1: viewPager.setCurrentItem(0); break; case R.id.btn2: viewPager.setCurrentItem(1); break; case R.id.btn3: viewPager.setCurrentItem(2); break; } } private class mPagerAdapter extends PagerAdapter { @Override public int getCount() { return pageViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override //使从ViewGroup中移出当前View public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(pageViews.get(arg1)); } @Override //返回一个对象,这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中 public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(pageViews.get(arg1)); return pageViews.get(arg1); } }}
3、BaseFragment代码
public abstract class BaseFragment extends Fragment { public Activity mActivity; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity=getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=initView(); return view; } /** 初始化布局 子类必须实现 */ public abstract View initView(); @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initData(); } public void initData() { } }
4、fragment代码
public class ContactsFragment extends BaseFragment { @Override public View initView() { View view=View.inflate(mActivity, R.layout.layout_contacts,null); return view; } } public class MessageFragment extends BaseFragment { @Override public View initView() { View view=View.inflate(mActivity, R.layout.layout_message,null); return view; } } public class TrendsFragment extends BaseFragment { @Override public View initView() { View view=View.inflate(mActivity, R.layout.layout_trends,null); return view; } }