Android中ViewPager和Fragment的使用

 更新时间:2016年06月11日 20:02:49   作者:guanhang89  
这篇文章主要介绍了Android中ViewPager和Fragment的使用方法介绍,感兴趣的朋友可以参考一下

GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
如果你想靠AI翻身,你先需要一个靠谱的工具!

小案例

XML中

1
2
3
4
5
<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

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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) {
 
  }
});

配合其他点击事件

1
2
3
4
5
6
7
8
9
10
11
12
//这里是注意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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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软件编程有所帮助。

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

相关文章

  • Android实现ViewFlipper图片动画滑动

    Android实现ViewFlipper图片动画滑动

    这篇文章主要为大家详细介绍了Android实现ViewFlipper图片动画滑动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Jetpack Compose图片组件使用实例详细讲解

    Jetpack Compose图片组件使用实例详细讲解

    在Compose中,图片组件主要有两种,分别是显示图标的Icon组件和显示图片的Image组件,当我们显示一系列的小图标的时候,我们可以使用Icon组件,当显示图片时,我们就用专用的Image组件
    2023-04-04
  • Android实现网易严选标签栏滑动效果

    Android实现网易严选标签栏滑动效果

    这篇文章主要为大家详细介绍了Android实现网易严选标签栏滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android自定义格式显示Button的布局思路

    Android自定义格式显示Button的布局思路

    下文的效果都是xml布局文件实现的,一张图片都未曾使用,顺便贴出几个布局文件留个大家参考下,感性的朋友可不要错过了哈
    2013-04-04
  • 实例讲解建立Android项目

    实例讲解建立Android项目

    在本篇内容中我们给大家整理了关于建立Android项目的步骤和相关知识点,有兴趣的读者们学习下。
    2019-03-03
  • Kotlin开发的一些实用小技巧总结

    Kotlin开发的一些实用小技巧总结

    Kotlin 是一个基于 JVM 的新编程语言,用 JetBrains 的话来说是「更现代化、更强大,所以下面这篇文章主要给大家总结介绍了关于Kotlin的一些开发实用小技巧,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • Android 自定义输入支付密码的软键盘实例代码

    Android 自定义输入支付密码的软键盘实例代码

    这篇文章主要介绍了Android 自定义输入支付密码的软键盘实例代码的相关资料,并附简单实例代码和实现效果图,需要的朋友可以参考下
    2016-11-11
  • Android实现绘制LocationMarkerView图的示例代码

    Android实现绘制LocationMarkerView图的示例代码

    LocationMarker是运动轨迹上Start、End, 以及整公里点上笔者自定义绘制的一个MarkerView。这篇文章主要介绍了Android实现绘制LocationMarkerView图的示例代码,希望对大家有所帮助
    2023-02-02
  • Qt5.12.6配置Android Arm开发环境(图文)

    Qt5.12.6配置Android Arm开发环境(图文)

    本文主要介绍了Qt5.12.6配置Android Arm开发环境,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • android studio生成aar包并在其他工程引用aar包的方法

    android studio生成aar包并在其他工程引用aar包的方法

    本篇文章主要介绍了android studio生成aar包并在其他工程引用aar包的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论