Activity/xml中
<?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:orientation="vertical"
tools:context=".fragment.TheFifthDayFragment">
<android.support.v4.view.ViewPager
android:id="@+id/vp_fifth"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<RadioGroup
android:id="@+id/mgrp"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<RadioButton
android:id="@+id/m_btn_one"
android:text="故事"
android:button="@null"
android:gravity="center"
android:layout_weight="3"
android:drawableTop="@drawable/rb_one"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:drawableTop="@drawable/rb_two"
android:id="@+id/m_btn_two"
android:text="亲子"
android:button="@null"
android:layout_weight="3"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
Activitity中
package
/**
* A simple {@link Fragment} subclass.
*/
public class TheFifthDayFragment extends Fragment {
@BindView(R.id.vp_fifth)
ViewPager vpFifth;
@BindView(R.id.m_btn_one)
RadioButton mBtnOne;
@BindView(R.id.m_btn_two)
RadioButton mBtnTwo;
@BindView(R.id.mgrp)
RadioGroup mgrp;
Unbinder unbinder;
private ArrayList<Fragment> list;
private OneFrament oneFrament;
private TwoFrament twoFrament;
public TheFifthDayFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_the_fifth_day, container, false);
unbinder = ButterKnife.bind(this, view);
initView();
return view;
}
private void initView() {
oneFrament = new OneFrament();
twoFrament = new TwoFrament();
list = new ArrayList<>();
list.add(oneFrament);
list.add(twoFrament);
VPandButtonadapter buttonadapter = new VPandButtonadapter(getChildFragmentManager(),
list);
vpFifth.setAdapter(buttonadapter);
vpFifth.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
mBtnOne.setChecked(true);
mBtnTwo.setChecked(false);
} else {
mBtnTwo.setChecked(true);
mBtnOne.setChecked(false);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.m_btn_one:
vpFifth.setCurrentItem(0,true);
break;
case R.id.m_btn_two:
vpFifth.setCurrentItem(1,true);
}
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
适配器 中
public class VPandButtonadapter extends FragmentPagerAdapter {
private ArrayList<Fragment> list;
public VPandButtonadapter(FragmentManager fm, ArrayList<Fragment> list) {
super(fm);
this.list = list;
}
public VPandButtonadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
网友评论