import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.lbs.zuo_yue.R;
import com.lbs.zuo_yue.chilfrags.cf1;
import com.lbs.zuo_yue.chilfrags.cf2;
import com.lbs.zuo_yue.chilfrags.cf3;
import com.lbs.zuo_yue.chilfrags.cf4;
/**
* author:Created by WangZhiQiang on 2017/11/16.
*/
public class f1 extends Fragment {
private ViewPager viewPager;
private RadioGroup chil_rg;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f1_view, container, false);
viewPager = view.findViewById(R.id.chil_vp);
chil_rg = view.findViewById(R.id.chil_rg);
myvpager mvp = new myvpager(getChildFragmentManager());
viewPager.setAdapter(mvp);
getchil_rg();
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
RadioButton chil_rb1 = view.findViewById(R.id.chil_rb1);
chil_rb1.setChecked(true);
break;
case 1:
RadioButton chil_rb2 = view.findViewById(R.id.chil_rb2);
chil_rb2.setChecked(true);
break;
case 2:
RadioButton chil_rb3 = view.findViewById(R.id.chil_rb3);
chil_rb3.setChecked(true);
break;
case 3:
RadioButton chil_rb4 = view.findViewById(R.id.chil_rb4);
chil_rb4.setChecked(true);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
class myvpager extends FragmentPagerAdapter {
public myvpager(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
cf1 cf1 = new cf1();
return cf1;
case 1:
cf2 cf2 = new cf2();
return cf2;
case 2:
cf3 cf3 = new cf3();
return cf3;
case 3:
cf4 cf4 = new cf4();
return cf4;
}
return null;
}
@Override
public int getCount() {
return 4;
}
}
public void getchil_rg() {
chil_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb1:
viewPager.setCurrentItem(0);
break;
case R.id.rb2:
RadioButton chil_rb2 = view.findViewById(R.id.chil_rb2);
chil_rb2.setChecked(true);
viewPager.setCurrentItem(1);
break;
case R.id.rb3:
RadioButton chil_rb3 = view.findViewById(R.id.chil_rb3);
chil_rb3.setChecked(true);
viewPager.setCurrentItem(3);
break;
case R.id.rb4:
RadioButton chil_rb4 = view.findViewById(R.id.chil_rb4);
chil_rb4.setChecked(true);
viewPager.setCurrentItem(4);
break;
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chil_vp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/chil_rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="chil_frag1"
android:id="@+id/chil_rb1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chil_frag2"
android:layout_weight="1"
android:id="@+id/chil_rb2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chil_frag3"
android:layout_weight="1"
android:id="@+id/chil_rb3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chil_frag4"
android:layout_weight="1"
android:id="@+id/chil_rb4"/>
</RadioGroup>
</LinearLayout>