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

Android中ViewPager和Fragment的使用

程序员文章站 2024-03-02 17:13:34
小案例 xml中

小案例

xml中

<android.support.v4.view.viewpager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
</android.support.v4.view.viewpager>

创建fragment

 fragments = new arraylist<>();
 conversationfragment fragment1 = new conversationfragment();
 groupfragment fragment2 = new groupfragment();
 searchfragment fragment3 = new searchfragment();
 fragments.add(fragment1);
 fragments.add(fragment2);
 fragments.add(fragment3);
 adapter = new mainpageradapter(getsupportfragmentmanager(), fragments);
 viewpager.setadapter(adapter);

adapter

public class mainpageradapter extends fragmentpageradapter{

  list<fragment> fragmentlist;

  public mainpageradapter(fragmentmanager fm, list<fragment> fragmentlist) {
    super(fm);
    this.fragmentlist = fragmentlist;
  }

  @override
  public fragment getitem(int position) {
    return fragmentlist.get(position);
  }

  @override
  public int getcount() {
    return fragmentlist.size();
  }
}

onpagechangelistener

viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() {
  @override
  public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {
    int distance = positionoffsetpixels / 3;
    //一旦fragment滑动,这里的position实际是前一个的
    viewpropertyanimator.animate(v_indicate_line).translationx(distance + position * v_indicate_line.getwidth()).setduration(0);
  }

  @override
  public void onpageselected(int position) {
    textlightandsize();
  }


  @override
  public void onpagescrollstatechanged(int state) {

  }
});

配合其他点击事件

//这里是注意setcurrentitem的用法
switch (view.getid()) {
  case r.id.ly_conversation:
    viewpager.setcurrentitem(0);
    break;
  case r.id.ly_group:
    viewpager.setcurrentitem(1);
    break;
  case r.id.ly_search:
    viewpager.setcurrentitem(2);
    break;
}

官方案例

r.layout.fragment_pager

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

  <android.support.v4.view.viewpager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="0px"
      android:layout_weight="1">
  </android.support.v4.view.viewpager>

  <linearlayout android:orientation="horizontal"
      android:gravity="center" android:measurewithlargestchild="true"
      android:layout_width="match_parent" android:layout_height="wrap_content"
      android:layout_weight="0">
    <button android:id="@+id/goto_first"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:text="@string/first">
    </button>
    <button android:id="@+id/goto_last"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:text="@string/last">
    </button>
  </linearlayout>
</linearlayout>

r.layout.fragment_pager_list

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:drawable/gallery_thumb">

  <textview android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:textappearance="?android:attr/textappearancemedium"
    android:text="@string/hello_world"/>

  <!-- the frame layout is here since we will be showing either
  the empty view or the list view. -->
  <framelayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >
    <!-- here is the list. since we are using a listactivity, we
       have to call it "@android:id/list" so listactivity will
       find it -->
    <listview android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:drawselectorontop="false"/>

    <!-- here is the view to show if the list is emtpy -->
    <textview android:id="@android:id/empty"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:textappearance="?android:attr/textappearancemedium"
      android:text="no items."/>

  </framelayout>

</linearlayout>

public class fragmentpagersupport extends fragmentactivity {
  static final int num_items = 10;

  myadapter madapter;

  viewpager mpager;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.fragment_pager);

    madapter = new myadapter(getsupportfragmentmanager());

    mpager = (viewpager)findviewbyid(r.id.pager);
    mpager.setadapter(madapter);

    // watch for button clicks.
    button button = (button)findviewbyid(r.id.goto_first);
    button.setonclicklistener(new onclicklistener() {
      public void onclick(view v) {
        mpager.setcurrentitem(0);
      }
    });
    button = (button)findviewbyid(r.id.goto_last);
    button.setonclicklistener(new onclicklistener() {
      public void onclick(view v) {
        mpager.setcurrentitem(num_items-1);
      }
    });
  }

  public static class myadapter extends fragmentpageradapter {
    public myadapter(fragmentmanager fm) {
      super(fm);
    }

    @override
    public int getcount() {
      return num_items;
    }

    @override
    public fragment getitem(int position) {
      return arraylistfragment.newinstance(position);
    }
  }

  public static class arraylistfragment extends listfragment {
    int mnum;

    /**
     * create a new instance of countingfragment, providing "num"
     * as an argument.
     */
    static arraylistfragment newinstance(int num) {
      arraylistfragment f = new arraylistfragment();

      // supply num input as an argument.
      bundle args = new bundle();
      args.putint("num", num);
      f.setarguments(args);

      return f;
    }

    /**
     * when creating, retrieve this instance's number from its arguments.
     */
    @override
    public void oncreate(bundle savedinstancestate) {
      super.oncreate(savedinstancestate);
      mnum = getarguments() != null ? getarguments().getint("num") : 1;
    }

    /**
     * the fragment's ui is just a simple text view showing its
     * instance number.
     */
    @override
    public view oncreateview(layoutinflater inflater, viewgroup container,
        bundle savedinstancestate) {
      view v = inflater.inflate(r.layout.fragment_pager_list, container, false);
      view tv = v.findviewbyid(r.id.text);
      ((textview)tv).settext("fragment #" + mnum);
      return v;
    }

    @override
    public void onactivitycreated(bundle savedinstancestate) {
      super.onactivitycreated(savedinstancestate);
      setlistadapter(new arrayadapter<string>(getactivity(),
          android.r.layout.simple_list_item_1, cheeses.scheesestrings));
    }

    @override
    public void onlistitemclick(listview l, view v, int position, long id) {
      log.i("fragmentlist", "item clicked: " + id);
    }
  }
}

注意

3.0之前的activity是不能用fragment的。为了能使用fragment(supportv4中),才有了fragmentactivity。fragmentactivity继承的activity。

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。