首先利用RadioGroup包裹着RadioButton
下面看下布局文件
<RadioGroup
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/home_attention"
android:layout_width="100px"
android:layout_height="match_parent"
android:button="@null"
android:text="关注"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/textcolor"
android:drawableBottom="@drawable/radiogroup_select"
/>
<RadioButton
android:id="@+id/home_hot"
android:layout_width="100px"
android:layout_height="match_parent"
android:button="@null"
android:text="热门"
android:gravity="center"
android:textSize="20sp"
android:checked="true"
android:layout_marginLeft="70px"
android:layout_marginRight="70px"
android:textColor="@drawable/textcolor"
android:drawableBottom="@drawable/radiogroup_select"
/>
<RadioButton
android:id="@+id/home_new"
android:layout_width="100px"
android:layout_height="match_parent"
android:button="@null"
android:text="最新"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/textcolor"
android:drawableBottom="@drawable/radiogroup_select"
/>
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/home_vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
/>
上面其中设置textcolor和drawableBottom设置的控件都是自定义的控件,下面贴出自定义控件
设置背景选择器,点击变色
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000" android:state_checked="false" android:state_enabled="true"></item>
<item android:color="#FF4081" android:state_checked="true" ></item>
</selector>
drawableBottom设置的自定义图标
也是一个背景选择器,点击设置背景,不点击不切换背景
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/xiahuaxian"/>
<item android:state_checked="false" android:drawable="@drawable/noxiahuaxian"/>
</selector>
设置下划线的自定义图片
选中
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@color/colorAccent"/>
<size android:height="5px"
android:width="320px"/>
未选中
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#f3f3f5"/>
<size android:height="5px"
android:width="320px"/>
</shape>
切换Fragment和viewpager之间的切换
public class HomeActivity extends FragmentActivity implements View.OnClickListener {
private RadioButton home_attention;
private RadioButton home_hot;
private RadioButton home_new;
private ViewPager home_vp;
private ArrayList<Fragment> FragmentList;
private Fragment attentionFragment;
private Fragment hotFragment;
private Fragment newFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initView();
initdata();
adpter();
home_vp.setCurrentItem(1);
}
private void initView() {
home_attention = (RadioButton) findViewById(R.id.home_attention);
home_hot = (RadioButton) findViewById(R.id.home_hot);
home_new = (RadioButton) findViewById(R.id.home_new);
home_vp = (ViewPager) findViewById(R.id.home_vp);
home_attention.setOnClickListener(this);
home_hot.setOnClickListener(this);
home_new.setOnClickListener(this);
}
private void adpter() {
home_vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return FragmentList.get(position);
}
@Override
public int getCount() {
return FragmentList.size();
}
});
//默认选择
home_hot.setChecked(true);
//设置滑动监听
home_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position==0){
home_attention.setChecked(true);
}else if (position==1){
home_hot.setChecked(true);
}else if (position==2){
home_new.setChecked(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initdata() {
FragmentList =new ArrayList<Fragment>();
attentionFragment = new AttentionFragment();
hotFragment = new HotFragment();
newFragment = new NewFragment();
FragmentList.add(attentionFragment);
FragmentList.add(hotFragment);
FragmentList.add(newFragment);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.home_attention:
home_vp.setCurrentItem(0);
break;
case R.id.home_hot:
home_vp.setCurrentItem(1);
break;
case R.id.home_new:
home_vp.setCurrentItem(2);
break;
}
}
}