ViewPager与Fragment结合使用,可滑动,可点击

布局文件
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="com.baiw.testdeom.View.activity.MainActivity">  
  8.   
  9.     <android.support.v4.view.ViewPager  
  10.         android:id="@+id/Main_Viewpager"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_weight="1"></android.support.v4.view.ViewPager>  
  14.   
  15.     <RadioGroup  
  16.         android:id="@+id/Main_RadioGroup"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:orientation="horizontal">  
  20.   
  21.         <RadioButton  
  22.             android:id="@+id/rb_01"  
  23.             android:layout_width="0dp"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_weight="1"  
  26.             android:button="@null"  
  27.             android:text="漫画"  
  28.             android:textSize="25sp" />  
  29.   
  30.         <RadioButton  
  31.             android:id="@+id/rb_02"  
  32.             android:layout_width="0dp"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_weight="1"  
  35.             android:button="@null"  
  36.             android:text="社区"  
  37.             android:textSize="25sp" />  
  38.   
  39.         <RadioButton  
  40.             android:id="@+id/rb_03"  
  41.             android:layout_width="0dp"  
  42.             android:layout_height="wrap_content"  
  43.             android:layout_weight="1"  
  44.             android:button="@null"  
  45.             android:text="会员"  
  46.             android:textSize="25sp" />  
  47.   
  48.         <RadioButton  
  49.             android:id="@+id/rb_04"  
  50.             android:layout_width="0dp"  
  51.             android:layout_height="wrap_content"  
  52.             android:layout_weight="1"  
  53.             android:button="@null"  
  54.             android:text="我的"  
  55.             android:textSize="25sp" />  
  56.   
  57.     </RadioGroup>  
  58.   
  59.   
  60. </LinearLayout>  


[html]  view plain  copy
  1. private void initView() {//找控件  
  2.        rg = (RadioGroup) findViewById(R.id.Main_RadioGroup);  
  3.        rg.setOnCheckedChangeListener(this);  
  4.        rb_1 = (RadioButton) findViewById(R.id.rb_01);  
  5.        rb_2 = (RadioButton) findViewById(R.id.rb_02);  
  6.        rb_3 = (RadioButton) findViewById(R.id.rb_03);  
  7.        rb_4 = (RadioButton) findViewById(R.id.rb_04);  
  8.        viewPager = (ViewPager) findViewById(R.id.Main_pager);  
  9.        //初始化适配器  
  10.        adapter = new MainPagerAdapter(getSupportFragmentManager());  
  11.        viewPager.setAdapter(adapter);  
  12.          
  13.        //viewpager的监听  
  14.        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
  15.            @Override  
  16.            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  17.   
  18.            }  
  19.   
  20.            @Override  
  21.            public void onPageSelected(int position) {//pager滑动的时候,根据角标radiobutton被选中  
  22.                switch (position) {  
  23.                    case 0:  
  24.                        rb_1.setChecked(true);  
  25.                        break;  
  26.                    case 1:  
  27.                        rb_2.setChecked(true);  
  28.                        break;  
  29.                    case 2:  
  30.                        rb_3.setChecked(true);  
  31.                        break;  
  32.                    case 3:  
  33.                        rb_4.setChecked(true);  
  34.                        break;  
  35.                }  
  36.   
  37.   
  38.            }  
  39.   
  40.            @Override  
  41.            public void onPageScrollStateChanged(int state) {  
  42.   
  43.            }  
  44.        });  
  45.    }  

初始化Fragment并添加到集合中
[html]  view plain  copy
  1. private void initEventes() {  
  2.     Fragment1 fragment1 = new Fragment1();  
  3.     Fragment2 fragment2 = new Fragment2();  
  4.     Fragment3 fragment3 = new Fragment3();  
  5.     Fragment4 fragment4 = new Fragment4();  
  6.     fragments.add(fragment1);  
  7.     fragments.add(fragment2);  
  8.     fragments.add(fragment3);  
  9.     fragments.add(fragment4);  
  10. }  


把集合传到适配器中
[html]  view plain  copy
  1. private void initGetDate() {  
  2.     adapter.getDate(fragments);  
  3.     adapter.notifyDataSetChanged();  
  4. }  

[html]  view plain  copy
  1. //点击rg可以跳转  
  2.    @Override  
  3.    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {  
  4.        int current = 0;  //初始值为0  
  5.        switch (checkedId) {  
  6.            case R.id.rb_01:  
  7.                current = 0;//第一个pager界面是0  
  8.                break;  
  9.   
  10.            case R.id.rb_02:  
  11.                current = 1;  
  12.                break;  
  13.            case R.id.rb_03:  
  14.                current = 2;  
  15.                break;  
  16.   
  17.            case R.id.rb_04:  
  18.                current = 3;  
  19.                break;  
  20.        }  
  21.        if (viewPager.getCurrentItem() != current) {  
  22.   
  23.            //vp.setCurrentItem(current);  
  24.            viewPager.setCurrentItem(current, false);  
  25.   
  26.        }  
  27.    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值