游标ViewPage+Fragment

来源:互联网 发布:mac book 能做什么 编辑:程序博客网 时间:2024/06/03 16:23

xml布局

<RadioGroup    android:id="@+id/radioGroup"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    >    <RadioButton        android:id="@+id/rad_but01"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="ANDROID"        android:gravity="center"        android:button="@null"        android:padding="10dp"        android:checked="true"        android:layout_weight="1"        />    <RadioButton        android:id="@+id/rad_but02"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="IOS"        android:gravity="center"        android:button="@null"        android:padding="10dp"        android:layout_weight="1"        />    <RadioButton        android:id="@+id/rad_but03"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="福利"        android:gravity="center"        android:button="@null"        android:padding="10dp"        android:layout_weight="1"        /></RadioGroup><LinearLayout    android:layout_width="match_parent"    android:layout_height="5dp"    android:orientation="horizontal"    >    <TextView        android:id="@+id/rad_text1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#5b5959"        android:layout_weight="1"        />    <TextView        android:id="@+id/rad_text2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#5b5959"        android:layout_weight="1"        />    <TextView        android:id="@+id/rad_text3"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#5b5959"        android:layout_weight="1"        /></LinearLayout><android.support.v4.view.ViewPager    android:id="@+id/home_viewpager"    android:layout_width="match_parent"    android:layout_height="match_parent"    />
MainActivity
 txt1.setVisibility(View.VISIBLE);    txt2.setVisibility(View.INVISIBLE);    txt3.setVisibility(View.INVISIBLE);    //将Fragment放入集合中    list = new ArrayList<Fragment>();    list.add(new Home_Android());    list.add(new Home_Ios());    list.add(new Home_Fuli());    //设置FragmentPageAdapter适配器将Fragemnt添加    viewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {        @Override        public Fragment getItem(int position) {            return list.get(position);        }        @Override        public int getCount() {            return list.size();        }    });    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {        @Override        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {        }        @Override        public void onPageSelected(int position) {            switch (position){                case 0:                    radioGroup.check(R.id.rad_but01);                    txt1.setVisibility(View.VISIBLE);                    txt2.setVisibility(View.INVISIBLE);                    txt3.setVisibility(View.INVISIBLE);                    break;                case 1:                    radioGroup.check(R.id.rad_but02);                    txt1.setVisibility(View.INVISIBLE);                    txt2.setVisibility(View.VISIBLE);                    txt3.setVisibility(View.INVISIBLE);                    break;                case 2:                    radioGroup.check(R.id.rad_but03);                    txt1.setVisibility(View.INVISIBLE);                    txt2.setVisibility(View.INVISIBLE);                    txt3.setVisibility(View.VISIBLE);            }        }        @Override        public void onPageScrollStateChanged(int state) {        }    });    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {            switch (checkedId){                case R.id.rad_but01 :                    viewPager.setCurrentItem(0);                    break;                case R.id.rad_but02 :                    viewPager.setCurrentItem(1);                    break;                case R.id.rad_but03 :                    viewPager.setCurrentItem(2);            }        }    });}

原创粉丝点击