activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <RadioGroup android:layout_alignParentBottom="true" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rb1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="button1" /> <RadioButton android:id="@+id/rb2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="button2" /> <RadioButton android:id="@+id/rb3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="button3" /> </RadioGroup> </RelativeLayout>
MainActivity
import android.graphics.Color; 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.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ArrayList<Fragment> fragments; private ViewPager vp; private RadioButton rb1, rb2, rb3; FragmentManager fm; Main1 m1; Main2 m2; Main3 m3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); fm = getSupportFragmentManager(); rb1 = (RadioButton) findViewById(R.id.rb1); rb2 = (RadioButton) findViewById(R.id.rb2); rb3 = (RadioButton) findViewById(R.id.rb3); rb1.setOnClickListener(this); rb2.setOnClickListener(this); rb3.setOnClickListener(this); m1 = new Main1(); m2 = new Main2(); m3 = new Main3(); fragments = new ArrayList<>(); fragments.add(m1); fragments.add(m2); fragments.add(m3); rb1.setBackgroundColor(Color.BLUE); MyAdapter adapter = new MyAdapter(fm, fragments); vp.setAdapter(adapter); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: rb1.setBackgroundColor(Color.BLUE); rb2.setBackgroundColor(Color.WHITE); rb3.setBackgroundColor(Color.WHITE); break; case 1: rb1.setBackgroundColor(Color.WHITE); rb2.setBackgroundColor(Color.RED); rb3.setBackgroundColor(Color.WHITE); break; case 2: rb1.setBackgroundColor(Color.WHITE); rb2.setBackgroundColor(Color.WHITE); rb3.setBackgroundColor(Color.GREEN); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.rb1: vp.setCurrentItem(0); rb1.setChecked(true); rb2.setChecked(false); rb3.setChecked(false); break; case R.id.rb2: vp.setCurrentItem(1); rb1.setChecked(false); rb2.setChecked(true); rb3.setChecked(false); break; case R.id.rb3: vp.setCurrentItem(2); rb1.setChecked(false); rb2.setChecked(false); rb3.setChecked(true); break; } } class MyAdapter extends FragmentPagerAdapter { private ArrayList<Fragment> fragments; public MyAdapter(FragmentManager fm, ArrayList<Fragment> fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } } }
Main2
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by lenovo on 2017/09/19. */ public class Main2 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.main2, null); return view; } }