Viewpager+radiogroup+fragment

fragment1.xml  其餘同樣java

<?xml version="1.0" encoding="utf-8"?>android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"ide

    android:layout_width="match_parent"佈局

    android:layout_height="match_parent"this

    android:gravity="center"3d

    android:orientation="vertical" >orm

    <TextViewxml

        android:layout_width="wrap_content"對象

        android:layout_height="wrap_content"blog

        android:text="第一頁"

        android:textSize="30sp"/>

</LinearLayout>

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <android.support.v4.view.ViewPager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"/>
  <View
  android:id="@+id/divider"
  android:layout_width="50dp"
  android:layout_height="3dp"
  android:background="@android:color/black"/>
  <RadioGroup
  android:id="@+id/radioGroup"
  android:layout_width="match_parent"
  android:layout_height="60dp"
  android:gravity="center_vertical"
  android:orientation="horizontal"
  >

    <RadioButton
    android:id="@+id/btn1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:gravity="center"
    android:text="第一頁"
    android:clickable="true"
    android:textColor="@color/btn"
    android:textSize="30sp" />

    <RadioButton
    android:id="@+id/btn2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:text="第二頁"
    android:clickable="true"
    android:textColor="@color/btn"
    android:gravity="center"
    android:textSize="30sp" />
    <RadioButton
    android:id="@+id/btn3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:button="@null"
    android:text="第三頁"
    android:clickable="true"
    android:textColor="@color/btn"
    android:gravity="center"
    android:textSize="30sp" />

  </RadioGroup>
</LinearLayout>

 

Fragment1.java
public class Fragment1 extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater,
  @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  return inflater.inflate(R.layout.fragment1, null);
  }
}

代碼實現
public class MainActivity extends FragmentActivity {

  private View line;// 指示線
  private int screenWidth;// 屏幕的寬度
  private ViewPager viewpager;
  private RadioGroup radioGroup;
  private RadioButton btn1,btn2,btn3;
  private FragmentTransaction fragmentTransaction;
  private List<Fragment> fragmentList;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 初始化界面
    viewpager = (ViewPager) findViewById(R.id.viewpager);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    btn1 = (RadioButton) findViewById(R.id.btn1);
    btn2 = (RadioButton) findViewById(R.id.btn2);
    btn3 = (RadioButton) findViewById(R.id.btn3);
    line = findViewById(R.id.divider);

    // 獲取片斷管理器
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    // 界面顯示時 默認按鈕1被選擇及顯示第一個界面
    btn1.setChecked(true);
    fragmentTransaction.replace(R.id.viewpager, new Fragment1()).commit();
    // fragmentList用於存放片斷 及 將片斷添加進集合
    fragmentList = new ArrayList<Fragment>();
    Fragment fragment1 = new Fragment1();
    Fragment fragment2 = new Fragment2();
    Fragment fragment3 = new Fragment3();
    fragmentList.add(fragment1);
    fragmentList.add(fragment2);
    fragmentList.add(fragment3);

    // 獲取屏幕的寬度
    DisplayMetrics outMetrics = new DisplayMetrics();
    getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
    screenWidth = outMetrics.widthPixels;
    // 設置指示線的寬度 獲取控件的(注意:必定要用父控件的LayoutParams寫LinearLayout.LayoutParams)
    LinearLayout.LayoutParams lp = (LayoutParams) line.getLayoutParams();// 獲取控件的佈局參數對象
    lp.width = screenWidth / fragmentList.size();
    line.setLayoutParams(lp);// 設置該控件的layoutParams參數

    // 對viewPager設置適配器FragmentPagerAdapter
    viewpager.setAdapter(new MyFragmentPagerAdapter(fragmentManager,fragmentList));
    // 對按鈕設置監聽 RadioButton切換Fragment
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
          case R.id.btn1:
            viewpager.setCurrentItem(0, true);
            break;
          case R.id.btn2:
            viewpager.setCurrentItem(1, true);
            break;
          case R.id.btn3:
            viewpager.setCurrentItem(2, true);
            break;
        }
      }
    });

  // 對viewpager設置監聽 viewpager切換fragment radiobutton作相應的改變
  viewpager.setOnPageChangeListener(new OnPageChangeListener() {

  // 當新的頁面被選中時調用
  @Override
  public void onPageSelected(int position) {
    // ((RadioButton)radioGroup.getChildAt(position)).performClick();
      switch (position) {
        case 0:
          radioGroup.check(R.id.btn1);
          break;
        case 1:
          radioGroup.check(R.id.btn2);
          break;
        case 2:
          radioGroup.check(R.id.btn3);
          break;
      }
  }

  /**
  * 當前頁面被滑動時調用 position:當前頁面 positionOffset:當前頁面偏移的百分比
  * positionOffsetPixels:當前頁面偏移的像素位置
  */
  @Override
  public void onPageScrolled(int position, float positionOffset,
  int positionOffsetPixels) {
    LinearLayout.LayoutParams lp = (LayoutParams) line.getLayoutParams();
    // 獲取組件距離左側組件的距離
    lp.leftMargin = (int) ((positionOffset + position)* lp.width);
      line.setLayoutParams(lp);
  }

  /**
  * 當滑動狀態改變時調用 state=0的時候表示什麼都沒作,就是停在那 state=1的時候表示正在滑動
  * state==2的時候表示滑動完畢了
  */
  @Override
  public void onPageScrollStateChanged(int state) {

  }
 });

private class MyFragmentPagerAdapter extends FragmentPagerAdapter {   List<Fragment> list;   public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {     super(fm);     this.list = list;   }   @Override   public Fragment getItem(int position) {     return list.get(position);   }   @Override   public int getCount() {     return list.size();   } }

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息