Android中ViewPager和Fragment的使用

Android中ViewPager和Fragment的使用,第1张

概述小案例XML中<android.support.v4.view.ViewPagerandroid:id=\"@+id/viewPager\"android:layout_width=\"match_parent\"

小案例

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软件编程有所帮助。

总结

以上是内存溢出为你收集整理的Android中ViewPager和Fragment的使用全部内容,希望文章能够帮你解决Android中ViewPager和Fragment的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141997.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存