请问如何点击上面的RadioButton切换底部的ViewPager,或者滑动底部ViewPager的时候选中顶部的RadioButton
public class ChannelFragment extends Fragment {
@BindView(R.id.rg_tabs)
RadioGroup mRadioGroup;
@BindView(R.id.vp_channel)
ViewPager mViewPager;
String[] topTitles = {"比赛", "解说", "专栏", "战队", "大神", "比赛", "解说", "专栏", "战队", "大神", "比赛", "解说", "专栏", "战队", "大神"};
private List<ChannelListBean> mDatas;
private ChannelAdapter mChannelAdapter;
private ChannelItemAdapter mChannelItemAdapter;
private List<TabController> mPageDatas;
//记录当前选中的tab
private int mCurrentTab;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_channel, null);
ButterKnife.bind(this, view);
initData();
initEvent();
return view;
}
private void initData() {
Bitmap a=null;
//顶部按钮
for (int i = 0; i < topTitles.length; i++) {
RadioButton tempButton = new RadioButton(getActivity());
tempButton.setButtonDrawable(null); // 设置按钮的样式
tempButton.setText(topTitles[i]);
tempButton.setButtonDrawable(new BitmapDrawable(a));
tempButton.setPadding(30, 10, 30, 10);
tempButton.setBackgroundResource(R.drawable.channel_title_selector);
tempButton.setGravity(Gravity.CENTER);
tempButton.setTextColor(R.drawable.channel_text_color_selector);
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(15, 0, 15, 0);//设置RadioButton的边距
mRadioGroup.addView(tempButton, lp);
if (i == 0) {
mRadioGroup.check(tempButton.getId());
}
}
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO 临时选中
final RadioButton tempButton = getActivity().findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
final CharSequence buttonText = tempButton.getText();
mCurrentTab = checkedId;
tempButton.setOnClickListener(new View.OnClickListener() {//获取每一个RadioButton的点击事件
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "点击了" + tempButton.getText(), Toast.LENGTH_SHORT).show();
if (buttonText.equals("比赛")) {
mCurrentTab = 0;
} else if (buttonText.equals("解说")) {
mCurrentTab = 1;
}
}
});
}
});
mPageDatas = new ArrayList<>();
//加载页面数据
mPageDatas = new ArrayList<>();
mPageDatas.add(new GameController(getActivity()));//比赛
mPageDatas.add(new CommentaryController(getActivity()));//比赛
//给ViewPager设置Adapter
mViewPager.setAdapter(new ContentPagerAdapter());
}
private void initEvent() {
}
/**
* 整体ViewPager的Adapter
*/
private class ContentPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
if (mPageDatas != null) {
return mPageDatas.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
BaseController controller = mPageDatas.get(position);
View rootView = controller.getRootView();
container.addView(rootView);
controller.initData();
return rootView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}