ViewPager简单使用

Viewpage的使用有两种方式,这里只写了一种方式,与fragment结合使用的。另外一种是与view结合使用的自己去网上查。

主页面xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/bg" >
    <include 
        layout="@layout/common_title"
        />
    <RadioGroup 
        android:id="@+id/rg_depa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/white"
        >
        <RadioButton 
            android:id="@+id/rb_depa"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="找科室"
            android:textSize="14sp"
            android:button="@null"
            
            android:gravity="center"
            android:background="@color/white"
            android:textColor="@drawable/textcolor_firstvisit_selector"
            />
        <View 
            android:layout_width="1dp"
            android:layout_height="20dp"
            android:background="@color/xian_gray"
            android:layout_gravity="center"
            />
        <RadioButton 
            android:id="@+id/rb_doctor"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="找医生"
            android:textSize="14sp"
            android:button="@null"
            android:gravity="center"
            android:background="@color/white"
            android:textColor="@drawable/textcolor_firstvisit_selector"
            />
    </RadioGroup>
    
      <android.support.v4.view.ViewPager
        android:id="@+id/depadoctorpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </android.support.v4.view.ViewPager>

</LinearLayout>

 //找到viewpage这个控件就不用说了。
<pre name="code" class="html">	@BindView(id=R.id.depadoctorpager,canClick=true)
	private ViewPager pager; 

 
 

1.首先创建fragment碎片存放起来

		HashMap map=new HashMap<Integer, Fragment>();
		Fragmentone pageone=new Fragmentone();
		Fragmenttwo pagetwo=new Fragmenttwo();
		map.put(0, pageone);
		map.put(1, pagetwo);
2.设置 一个page适配器,FragmentPagerAdapter

		MyFragmentPagerAdapter pageadapter =new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentmap);//第一步
		pager.setAdapter(pageadapter);
 
  
 
	class MyFragmentPagerAdapter extends FragmentPagerAdapter{
		Map<Integer,Fragment> map;
		public MyFragmentPagerAdapter(FragmentManager fm,Map<Integer,Fragment> map) {
			super(fm);
			this.map=map;
		}

		@Override
		public Fragment getItem(int arg0) {
			return map.get(arg0);
		}

		@Override
		public int getCount() {
			return map.size();
		}
	}



3.设置viewpage的滑动监听事件和按钮点击事件

		pager.setOnPageChangeListener(new OnPageChangeListener() {//第三部
			@Override
			public void onPageSelected(int arg0) {
				type=arg0;
				if(arg0==0){
					rb_depa.setBackgroundResource(R.drawable.iv_slidingline);
					rb_doctor.setBackgroundResource(0);
					rb_depa.setChecked(true);
					rb_doctor.setChecked(false);
					et_searchdepa.setHint("请输入科室名称");
				}else{
					rb_depa.setBackgroundResource(0);
					rb_doctor.setBackgroundResource(R.drawable.iv_slidingline);
					rb_depa.setChecked(false);
					rb_doctor.setChecked(true);
					et_searchdepa.setHint("请输入科室或医生名称");
				}
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
//初始化的时候必须要设置一下默认选择的页面
<pre name="code" class="html"><span style="white-space:pre">		</span><span style="color:#ff0000;">pager.setCurrentItem(0);</span>
		rb_depa.setChecked(true);
		rb_doctor.setChecked(false);
		rb_depa.setBackgroundResource(R.drawable.iv_slidingline);
		rb_doctor.setBackgroundResource(0);
		et_searchdepa.setHint("请输入科室名称");


 
 

<span style="white-space:pre">	</span>button.setOnCheckedChangeListener(new OnchengeLister());
	private class OnchengeLister implements OnCheckedChangeListener{
		@Override
		public void onCheckedChanged(RadioGroup arg0, int arg1) {
			switch (arg1) {
			case R.id.rb_depa:
				pager.setCurrentItem(0);
				break;
			case R.id.rb_doctor:
				pager.setCurrentItem(1);
				break;
			default:
				break;
			}
		}
	}
好了,写的有点乱,我自己认识




你可能感兴趣的:(viewpager)