<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.a.yuekaob.Main2Activity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/RradioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:orientation="horizontal"> <RadioButton android:id="@+id/Radio_one" android:layout_width="30dp" android:layout_height="30dp" android:button="@null" /> <RadioButton android:id="@+id/Radio_Two" android:layout_width="30dp" android:layout_height="30dp" android:button="@null" /> <RadioButton android:id="@+id/Radio_Three" android:layout_width="30dp" android:layout_height="30dp" android:button="@null" /> </RadioGroup> </RelativeLayout>
public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private ViewPager viewpager; private RadioGroup RradioGroup; private Button mBtn; private ArrayList<View> list = new ArrayList<>(); private SharedPreferences mShare; private SharedPreferences.Editor mEditor; private Context mContext = Main2Activity.this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); } private void initView() { viewpager = (ViewPager) findViewById(R.id.viewpager); RradioGroup = (RadioGroup) findViewById(R.id.RradioGroup); View one = getLayoutInflater().inflate(R.layout.view_one, null); View two = getLayoutInflater().inflate(R.layout.view_two, null); View three = getLayoutInflater().inflate(R.layout.view_three, null); mBtn = three.findViewById(R.id.ViewThree_Btn); list.add(one); list.add(two); list.add(three); viewpager.setOnClickListener(this); PagAdaptera adapter = new PagAdaptera(list); viewpager.setAdapter(adapter); mBtn.setOnClickListener(this); RradioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.Radio_one: viewpager.setCurrentItem(0); break; case R.id.Radio_Two: viewpager.setCurrentItem(1); break; case R.id.Radio_Three: viewpager.setCurrentItem(2); break; } } }); viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { RadioButton rb = (RadioButton)RradioGroup.getChildAt(position); rb.setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.ViewThree_Btn: // mEditor.putBoolean("isTrue", true); // mEditor.commit(); Intent intent = new Intent(Main2Activity.this,MainActivity.class); startActivity(intent); break; } } }
public class PagAdaptera extends PagerAdapter { private ArrayList<View> list; public PagAdaptera(ArrayList<View> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }