欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Fragment滑动组件ViewPager的实例详解

程序员文章站 2023-11-26 22:33:04
android fragment滑动组件viewpager的实例详解 1适配器fragmentpageradapter的实现 对于fragmentpageradapte...

android fragment滑动组件viewpager的实例详解

1适配器fragmentpageradapter的实现

对于fragmentpageradapter的派生类,只需要重写getitem(int)和getcount()就可以了。

public class myfragmentpageradapter extends fragmentpageradapter {
  private list<fragment> list; 
  public myfragmentpageradapter(fragmentmanager fm, list<fragment> list) { 
    super(fm); 
    this.list = list;
  }
  //返回了当前要滑动的view的个数 
  @override 
  public int getcount() { 
    return list.size(); 
  } 
  //在getitem(int arg0)中,根据传来的参数arg0,来返回当前要显示的fragment
  @override 
  public fragment getitem(int arg0) { 
    return list.get(arg0); 
  }
}

2两个fragment类

orderfragment.java

public class orderfragment extends fragment {
  //在oncreateview()中返回要显示的view
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container,
      bundle savedinstancestate) {
    // todo auto-generated method stub
    view view= inflater.inflate(r.layout.fragment_order, container, false);
    //view中控件的操作方法
    refresh = view.findviewbyid(r.id.order_count_refresh);
    refresh.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        //操作逻辑
        countstatus();
      }
    });
    return view;
  }
}

fragment_order.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:background="#ffffff"
  android:orientation="vertical" >

  <button android:id="@+id/fragment1_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="show toast"
    />
</linearlayout>

mefragment.java

public class mefragment extends fragment {
  //在oncreateview()中返回要显示的view
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container,
      bundle savedinstancestate) {
    // todo auto-generated method stub
    view view= inflater.inflate(r.layout.fragment_me, container, false);
    //view中控件的操作方法
    login = view.findviewbyid(r.id.me_login);
    login.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view v) {
        startactivity(new intent(mefragment.this.getactivity(),
            loginactivity.class));
      }
    });
    return view;
  }
}

fragment_me.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:background="#ffff00"
  android:orientation="vertical" >


</linearlayout>

3主activity实现

public class mainactivity extends fragmentactivity {
  private viewpager mpager;
  private list<fragment> fragmentlist;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    mpager = (viewpager) findviewbyid(r.id.viewpager);
    //初始化fragment
    list<fragment> fragmentlist=new arraylist<fragment>();
    fragmentlist.add(new orderfragment());
    fragmentlist.add(new mefragment());
    fragadapter adapter = new fragadapter(getsupportfragmentmanager(), fragments);

    //给viewpager设置适配器
    mpager.setadapter(new myfragmentpageradapter(getsupportfragmentmanager(), fragmentlist));
    mpager.setcurrentitem(0);// 设置当前显示标签页为第一页
    //myonpagechangelistener()是页面变化监听器
    mpager.setonpagechangelistener(new myonpagechangelistener());
    radiogroup = (radiogroup) findviewbyid(r.id.main_tab);
    //给radiogroup设置监听器
    radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(radiogroup group, int checkedid) {
        if(checkedid == r.id.tab_me){
          mpager.setcurrentitem(index_me);
        }else if(checkedid == r.id.tab_shoujian){
          mpager.setcurrentitem(index_shoujian);
        }
      }
    });
  }
  //实现页面变化监听器onpagechangelistener
  public class myonpagechangelistener implements onpagechangelistener {
    @override
    //当页面在滑动的时候会调用此方法,在滑动被停止之前,此方法会一直得到调用。
    /** 
      * arg0:当前页面,及你点击滑动的页面
      * arg1:当前页面偏移的百分比
      *arg2:当前页面偏移的像素位置
      */
    public void onpagescrolled(int arg0, float arg1, int arg2) {
      // todo auto-generated method stub
    }

    @override 
    //当页面状态改变的时候调用
     /** 
      * arg0 
      * 1:表示正在滑动 
      * 2:表示滑动完毕 
      * 0:表示什么都没做,就是停在那 
      */ 
    public void onpagescrollstatechanged(int arg0) {
      // todo auto-generated method stub
    }

    @override
    //页面跳转完后调用此方法
    /** 
     * arg0是页面跳转完后得到的页面的position(位置编号)。  
     */ 
    public void onpageselected(int arg0) {
      // todo auto-generated method stub
      switch (arg0) {
      case index_me:
        radiogroup.check(r.id.tab_me);
        break;
      case index_shoujian:
        radiogroup.check(r.id.tab_shoujian);
        break;
      }
    }
  }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
 <relativelayout 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android"> 

 <android.support.v4.view.viewpager 
    android:id="@+id/viewpager"
    android:layout_alignparenttop="true"
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginbottom="60dip"
    android:flipinterval="30" 
    android:persistentdrawingcache="animation"/> 

  <radiogroup 
    android:id="@+id/main_tab" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_alignparentbottom="true"
    android:layout_height="60dip"> 

    <radiobutton
      style="@style/main_tab"
      android:id="@+id/tab_shoujian" 
      <!--图片在文字的上面-->
      android:drawabletop="@drawable/tab_shoujian_selector"
      android:layout_height="match_parent"
      android:checked="true" 
      android:text="****" /> 
    <radiobutton
      style="@style/main_tab" 
      android:id="@+id/tab_me"   
      android:drawabletop="@drawable/tab_me_selector"
      android:layout_height="match_parent" 
      android:text="*" /> 
  </radiogroup> 
</relativelayout> 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!