<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context="com.walxy.a20170828test.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/qz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#e8061d"
android:button="@null"
android:gravity="center"
android:text="圈子"/>
<RadioButton
android:id="@+id/py"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#38d42f"
android:button="@null"
android:gravity="center"
android:text="朋友"/>
<RadioButton
android:id="@+id/wd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0524c0"
android:button="@null"
android:gravity="center"
android:text="我的"/>
</RadioGroup>
</LinearLayout>
package com.walxy.a20170828test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.walxy.a20170828test.adapter.FmentAdapter;
import com.walxy.a20170828test.fragment.FriendFment;
import com.walxy.a20170828test.fragment.MyFment;
import com.walxy.a20170828test.fragment.RoundFment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private ViewPager mVp;
private RadioButton mQz;
private RadioButton mPy;
private RadioButton mWd;
private RadioGroup mRg;
private FriendFment mFriendFment;
private RoundFment mRoundFment;
private MyFment mMyFment;
private List<Fragment> mFragmentList;
private FragmentManager mManager;
private FmentAdapter mFmentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initFragment();
}
private void initFragment() {
mFragmentList = new ArrayList<>();
mFriendFment = new FriendFment();
mRoundFment = new RoundFment();
mMyFment = new MyFment();
mFragmentList.add(mRoundFment);
mFragmentList.add(mFriendFment);
mFragmentList.add(mMyFment);
mManager = getSupportFragmentManager();
mFmentAdapter = new FmentAdapter(mManager, mFragmentList);
mVp.setAdapter(mFmentAdapter);
}
private void initView() {
mVp = (ViewPager) findViewById(R.id.vp);
mQz = (RadioButton) findViewById(R.id.qz);
mQz.setOnClickListener(this);
mPy = (RadioButton) findViewById(R.id.py);
mPy.setOnClickListener(this);
mWd = (RadioButton) findViewById(R.id.wd);
mWd.setOnClickListener(this);
mRg = (RadioGroup) findViewById(R.id.rg);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.qz:
mVp.setCurrentItem(0);
break;
case R.id.py:
mVp.setCurrentItem(1);
break;
case R.id.wd:
mVp.setCurrentItem(2);
break;
}
}
}
package com.walxy.a20170828test.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* 作者: walxy 2017/8/28 19:45
* 类的用途:
*/
public class FmentAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList;
public FmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
mFragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList != null ? mFragmentList.size() : 0;
}
}