关于分段选择大家肯定都接触过很多,今天就来实现一下。
老样子先看下效果图
看下如何实现的
在drawable中定义所需要的资源文件
customize_news_rb_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/customize_news_rb_bg_normal"
android:state_checked="false"/>
<item android:drawable="@drawable/customize_news_rb_bg_selected"
android:state_checked="true"/>
selector>
customize_news_rb_bg_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1dp"/>
<solid android:color="@android:color/transparent"/>
shape>
customize_news_rb_bg_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1dp"/>
<solid android:color="@android:color/transparent"/>
shape>
customize_news_default_rb_text.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FE7371"
android:state_checked="false"/>
<item android:color="@color/colorWhite"
selector>
接下来是布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorWhite">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorTitle">
<RadioGroup
android:id="@+id/news_rg_all"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/news_rb_paperstrip"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:background="@drawable/customize_news_rb_bg"
android:button="@null"
android:gravity="center"
android:text="纸条"
android:textColor="@drawable/customize_news_default_rb_text"
android:textSize="17sp"/>
<RadioButton
android:id="@+id/news_rb_circle"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/customize_news_rb_bg"
android:button="@null"
android:gravity="center"
android:checked="true"
android:text="圈子"
android:textColor="@drawable/customize_news_default_rb_text"
android:textSize="17sp"/>
<RadioButton
android:id="@+id/news_rb_like"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="50dp"
android:background="@drawable/customize_news_rb_bg"
android:button="@null"
android:gravity="center"
android:text="喜欢"
android:textColor="@drawable/customize_news_default_rb_text"
android:textSize="17sp"/>
RadioGroup>
<ImageView
android:id="@+id/news_img_add_chat"
android:layout_width="47dp"
android:layout_height="47dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/ic_menu_add_chat"/>
RelativeLayout>
<FrameLayout
android:id="@+id/news_frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
顶部用了一个相对布局并且设置了高度为50dp以及一个背景色,RadioGroup设置为横向和下面是自定义的RadioButton,第二个RadioButton我设置为选择状态,android:checked=”true”,FrameLayout是用来显示Fragment之间切换内容的。
我是在Fragment里面使用的,用法和在Activity中是一样的,代码如下。
package com.ranlegeran.simple.fragment;
import android.os.Bundle;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ranlegeran.simple.R;
import com.ranlegeran.simple.news.fragment.CircleFragment;
import com.ranlegeran.simple.news.fragment.LikeFragment;
import com.ranlegeran.simple.news.fragment.PaperstripFragment;
import com.ranlegeran.simple.utils.T;
import butterknife.ButterKnife;
/**
* 消息
*/
public class NewsFragment extends Fragment {
private RadioGroup mRadioGroupAll;
private PaperstripFragment mPaperstripFragment;
private CircleFragment mCircleFragment;
private LikeFragment mLikeFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_news,container,false);
mRadioGroupAll = (RadioGroup) view.findViewById(R.id.news_rg_all);
initView();
selectFragment(0);
ButterKnife.bind(this,view);
return view;
}
private void initView() {
mRadioGroupAll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if (checkedId == R.id.news_rb_paperstrip) {
selectFragment(0);
} else if (checkedId == R.id.news_rb_circle) {
selectFragment(1);
} else if (checkedId == R.id.news_rb_like) {
selectFragment(2);
}
}
});
}
private void selectFragment(int position) {
FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
hideFragment(mFragmentTransaction);
if (position == 0) {
if (mPaperstripFragment == null) {
mPaperstripFragment = new PaperstripFragment();
mFragmentTransaction.add(R.id.news_frame_content, mPaperstripFragment);
} else {
mFragmentTransaction.show(mPaperstripFragment);
}
} else if (position == 1) {
if (mCircleFragment == null) {
mCircleFragment = new CircleFragment();
mFragmentTransaction.add(R.id.news_frame_content, mCircleFragment);
} else {
mFragmentTransaction.show(mCircleFragment);
}
} else if (position == 2) {
if (mLikeFragment == null) {
mLikeFragment = new LikeFragment();
mFragmentTransaction.add(R.id.news_frame_content, mLikeFragment);
} else {
mFragmentTransaction.show(mLikeFragment);
}
}
mFragmentTransaction.commit();
}
private void hideFragment(FragmentTransaction mFragmentTransaction) {
if (mPaperstripFragment != null) {
mFragmentTransaction.hide(mPaperstripFragment);
}
if (mCircleFragment != null) {
mFragmentTransaction.hide(mCircleFragment);
}
if (mLikeFragment != null) {
mFragmentTransaction.hide(mLikeFragment);
}
}
}
为了方便我只贴一个Fragment出来,其他两个都是一样的,布局很简单只放了一个TextView用来显示文字。
PaperstripFragment代码如下
package com.ranlegeran.simple.news.fragment;
import android.os.Bundle;
import com.ranlegeran.simple.R;
import com.ranlegeran.simple.base.BaseFragment;
/**
* 创建日期:2018/9/4 on 16:34
* 作 者: RANLEGERAN
* 类 名: Fragment
* 简 述:消息 纸条Fragment
*/
public class PaperstripFragment extends BaseFragment {
@Override
public int getLayoutResId() {
return R.layout.fragment_paperstrip_layout;
}
@Override
public void init(Bundle savedInstanceState) {
}
}
fragment_paperstrip_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="木有消息哦~"
android:textSize="17sp"
android:textColor="#9A9A9A"/>
RelativeLayout>
好啦,以上是全部代码,直接复制即可使用。