当前位置: 代码迷 >> 综合 >> ViewPager,RadioGroup,FragmentManager 详解 二 主菜单 RadioGroup 和ViewPager 实现联动
  详细解决方案

ViewPager,RadioGroup,FragmentManager 详解 二 主菜单 RadioGroup 和ViewPager 实现联动

热度:15   发布时间:2023-12-16 15:45:07.0

0:首先创建3个Fragment ,这写Fragment 必须是v4包里面的

1:然后布局里面添加ViewPager和RadioGroup,RadioGroup的orientation 一定要是horization

ViewPage 一定要写全路径

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:orientation="vertical"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><android.support.v4.view.ViewPager  android:layout_width="match_parent"  android:layout_height="0dp"  android:layout_weight="1"  android:id="@+id/viewPager"></android.support.v4.view.ViewPager><RadioGroup  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="horizontal"  android:id="@+id/radiogroup"><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="a"  android:textSize="35dp"  android:textAlignment="center"  android:id="@+id/radiobutton_a"/><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="b"  android:textSize="35dp"  android:textAlignment="center"  android:id="@+id/radiobutton_b"/><RadioButton  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="a"  android:textSize="35dp"  android:textAlignment="center"  android:id="@+id/radiobutton_c"/></RadioGroup></LinearLayout>

2:创建Adapter,和Fragment 相关的ViewPager的adapte 是FragmentPagerAdapter或者是FragmentStatePagerAdapter,如果是后者的话默认的就是加载3个Fragment,前后和当前的,其他的Fragmet 都是销毁的。我们这里使用FragmentPagerAdapter

package tech.androidstudio.fragmentviewpagerradiogroup.adapter;import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/**  * Created by Kodulf on 2016/3/14.  */ public class RadioGroupFragmentPagerAdapter extends FragmentPagerAdapter {List<Fragment> list;//创建的构造方法一定要包含List<Fragment>的参数,  //FragmentManager 一定要是v4包里面的  public RadioGroupFragmentPagerAdapter(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();}
}

2:在MainActivty 里面实现联动。

package tech.androidstudio.fragmentviewpagerradiogroup;import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;import java.util.ArrayList;
import java.util.List;import tech.androidstudio.fragmentviewpagerradiogroup.adapter.RadioGroupFragmentPagerAdapter;public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {private ViewPager mViewPager;private RadioGroup mRadioGroup;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Fragment 的列表,将会显示在ViewPager里面。  List<Fragment> mList = new ArrayList<Fragment>();mList.add(new BlankFragment());mList.add(new SecondFragment());mList.add(new ThirdFragment());//Adapter设置,这里面的 FragmentManager 一定要是支持包里面的SupportFragmentManager  RadioGroupFragmentPagerAdapter adapter = new RadioGroupFragmentPagerAdapter(getSupportFragmentManager(),mList);mViewPager = (ViewPager) findViewById(R.id.viewPager);mViewPager.setAdapter(adapter);mRadioGroup = (RadioGroup)findViewById(R.id.radiogroup);//设置联动,设置ViewPagerPageChange的监听器,RadioGroupCheckedChange监听器  mViewPager.setOnPageChangeListener(this);mRadioGroup.setOnCheckedChangeListener(this);//默认的是 让第一个 RadioButton 是选中的状态  mRadioGroup.check(R.id.radiobutton_a);}@Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}//ViewPager 翻页以后,RadioGroup 也进行 相应的操作。  @Override
    public void onPageSelected(int position) {switch (position){case 0:mRadioGroup.check(R.id.radiobutton_a);break;case 1:mRadioGroup.check(R.id.radiobutton_b);break;case 2:mRadioGroup.check(R.id.radiobutton_c);break;}}@Override
    public void onPageScrollStateChanged(int state) {}//RadioGroup 点击之后切换viewPager  @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.radiobutton_a:mViewPager.setCurrentItem(0);break;case R.id.radiobutton_b:mViewPager.setCurrentItem(1);break;case R.id.radiobutton_c:mViewPager.setCurrentItem(2);break;}}
}





  相关解决方案