package com.myviewpager;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.ActivityGroup;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
@SuppressWarnings("deprecation")
public class MainActivity extends ActivityGroup {
private ViewPager viewpager;
private List<View> mList;
private ViewPagerAdapter adapter;
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radiogroup = (RadioGroup) findViewById(R.id.main_radiogroup);
viewpager = (ViewPager) findViewById(R.id.viewpager);
mList = new ArrayList<View>();
View a = getLocalActivityManager().startActivity("a",
new Intent(this, Aactivity.class)).getDecorView();
View b = getLocalActivityManager().startActivity("b",
new Intent(this, Bactivity.class)).getDecorView();
View c = getLocalActivityManager().startActivity("c",
new Intent(this, Cactivity.class)).getDecorView();
mList.add(a);
mList.add(b);
mList.add(c);
adapter = new ViewPagerAdapter(mList);
viewpager.setAdapter(adapter);
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radiobutton1:
viewpager.setCurrentItem(0);
break;
case R.id.raidobutton2:
viewpager.setCurrentItem(1);
break;
case R.id.radiobutton3:
viewpager.setCurrentItem(2);
break;
default:
break;
}
}
});
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
((RadioButton) radiogroup.getChildAt(arg0)).setChecked(true);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private class ViewPagerAdapter extends PagerAdapter {
private List<View> view;
private ViewPagerAdapter(List<View> view) {
this.view = view;
}
@Override
public int getCount() {
return view.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(view.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(view.get(position));
return view.get(position);
}
}
}
ViewPager+RadioButton
最新推荐文章于 2019-05-28 16:08:53 发布