资讯专栏INFORMATION COLUMN

ViewPager的使用

netScorpion / 844人阅读

摘要:写到这里我忽然忘记我们把笔记详情页给忘了,没有它我们该拿什么去显示笔记的详情呢,好吧,赶快去新建一个命名为上面的方法和之前的所使用的技巧是一样的。从中启动传参的方法是,自然的,我们就有了这个常量。

一 前言

上次我们用RecyclerView做了一个简单的显示笔记的小程序,今天我们用ViewPager来扩展它的功能:当我们点击笔记列表的其中某条笔记时,它可以跳到另外一个页面完整的显示这条笔记的内容,更人性化的设计是,在某条笔记的详情页面,我们可以左右滑动以查看上一条或者下一条,而不是返回主列表再去选择,话不多说,操作起来!

二 准备工作

1.首先我们创建一个Activity,命名为NotePagerActivity
2.定义NotePagerActivity的私有字段:

</>复制代码

  1. public class NotePagerActivity extends AppCompatActivity {
  2. private static final String EXTRA_NOTE_ID = "com.aristark.note.note.id";
  3. private ViewPager noteViewPager;
  4. private ArrayList notes;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_note_pager);
  9. }
  10. }

因为我们是从列表Activity那里点击某一个笔记项才进入到NotePagerActivity,因此需要传入该笔记项的UUID(很容易想到两个Activity之间传递数据所使用的方法是Intent附加参数,也就是putExtra()方法,因此我们将该常量命名为EXTRA_NOTE_ID,养成这样良好的命名方法对以后读懂这段代码是很重要的!先不用好奇为什么要把它设为私有,一会儿就明白其中的妙处了),noteViewPager和notes自然不用多说,这是今天的主角。

3.在布局中activity_note_pager设置ViewPager
直接上代码

</>复制代码

4为NotePagerActivity编写newIntent方法

</>复制代码

  1. public static Intent newIntent(Context context,UUID uuid){
  2. Intent i = new Intent(context,NotePagerActivity.class);
  3. i.putExtra(EXTRA_NOTE_ID,uuid);
  4. return i;
  5. }

这样我们每次想要启动NotePagerActivity时只需调用这个静态方法,而不用再balabalabala重复一堆昨天的故事。

三 设置ViewPager

其实我也不知道为什么用设置这个词,应该用使用?装配?其实不需要太在意,我们的目的现在很简单,就是按照ViewPager给的接口传入相应的参数,让它工作起来就行(对新手而言)!
先贴上NoteListsFragment的代码

</>复制代码

  1. package com.aristark.note;

</>复制代码

  1. import android.os.Bundle;
  2. import android.support.v4.app.Fragment;
  3. import android.support.v7.widget.LinearLayoutManager;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.TextView;
  9. import java.util.ArrayList;
  10. import java.util.Calendar;
  11. import java.util.List;
  12. public class NotesListFragment extends Fragment {
  13. private RecyclerView noteRecycler;
  14. private NoteAdapter noteAdapter;

</>复制代码

  1. public NotesListFragment() {
  2. // Required empty public constructor
  3. }

</>复制代码

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. // Inflate the layout for this fragment
  5. // return inflater.inflate(R.layout.fragment_notes_list, container, false);
  6. View root = inflater.inflate(R.layout.fragment_notes_list,container,false);
  7. noteRecycler = (RecyclerView) root.findViewById(R.id.note_recycler_view);
  8. noteRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
  9. // NoteLab noteLab = NoteLab.getNoteLab(getActivity());
  10. // ArrayList notes = noteLab.getNotes();
  11. // noteAdapter = new NoteAdapter(notes);
  12. // noteRecycler.setAdapter(noteAdapter);
  13. updateView();
  14. return root;
  15. }
  16. @Override
  17. public void onResume() {
  18. super.onResume();
  19. updateView();
  20. }
  21. private class NoteHolder extends RecyclerView.ViewHolder{
  22. private TextView noteTitle;
  23. private TextView noteContent;
  24. private TextView noteDate;
  25. public NoteHolder(View root) {
  26. super(root);
  27. noteTitle = (TextView) root.findViewById(R.id.list_item_note_title);
  28. noteContent = (TextView) root.findViewById(R.id.list_item_note_content);
  29. noteDate = (TextView) root.findViewById(R.id.list_item_note_date);
  30. }
  31. public void bindView(Note n){
  32. this.note = n;
  33. noteTitle.setText(note.getTitle());
  34. noteContent.setText(note.getContent());
  35. Calendar calendar = Calendar.getInstance();
  36. calendar.setTime(note.getDate());
  37. int year = calendar.get(1);
  38. int day = calendar.get(5);
  39. int month = calendar.get(2)+1;
  40. String date = year+"年"+month+"月"+day+"日";
  41. noteDate.setText(date);
  42. }
  43. }
  44. private class NoteAdapter extends RecyclerView.Adapter{
  45. private List notes;
  46. public NoteAdapter(List notes){
  47. this.notes = notes;
  48. }
  49. public void setNotes(List notes) {
  50. this.notes = notes;
  51. }
  52. @Override
  53. public NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  54. LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
  55. View view = layoutInflater.inflate(R.layout.list_item_note,parent,false);
  56. return new NoteHolder(view);
  57. }
  58. @Override
  59. public void onBindViewHolder(NoteHolder holder, int position) {
  60. Note note = notes.get(position);
  61. holder.bindView(note);
  62. }
  63. @Override
  64. public int getItemCount() {
  65. return notes.size();
  66. }
  67. }
  68. public void updateView(){
  69. NoteLab noteLab = NoteLab.getNoteLab(getActivity());
  70. ArrayList notes = noteLab.getNotes();
  71. if (noteAdapter == null){
  72. noteAdapter = new NoteAdapter(notes);
  73. noteRecycler.setAdapter(noteAdapter);
  74. return;
  75. }
  76. noteAdapter.setNotes(notes);
  77. noteRecycler.setAdapter(noteAdapter);
  78. }
  79. }

和上回的代码略有不同,我做了一些小小的封装。我们把注意力集中到类NoteHolder的构造方法这里来,它传入的参数root的类型是View,也就是笔记列表项的每一个笔记记录,我们在这里设置一个监听器,当用户点击的时候,我们就让页面跳转到笔记详情页面,也就是CrimePagerActivity,来,写代码:

</>复制代码

  1. public NoteHolder(View root) {
  2. super(root);
  3. root.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. Intent i = NotePagerActivity.newIntent(getActivity(),note.getUuid());

</>复制代码

  1. startActivity(i);
  2. }
  3. });
  4. noteTitle = (TextView) root.findViewById(R.id.list_item_note_title);
  5. noteContent = (TextView) root.findViewById(R.id.list_item_note_content);
  6. noteDate = (TextView) root.findViewById(R.id.list_item_note_date);
  7. }

下面把注意力转移到CrimePagerActivity:
1.获取ViewPager:

</>复制代码

  1. noteViewPager = (ViewPager) findViewById(R.id.note_view_pager);

2.从传来的Intent里获取uuid,并以此uuid从全局静态对象NoteLab中获取notes:

</>复制代码

  1. UUID uuid = (UUID)getIntent().getSerializableExtra(EXTRA_NOTE_ID);
  2. notes = NoteLab.getNoteLab(this).getNotes();

这里再一次体会到了EXTRA_NOTE_ID的方便之处吧。
3.为ViewPager设置Adapter(ViewPager和RecyclerView一样,每个页面的布局都是一样的,只是填充的数据不一样,因为需要Adapter去适配,这里感叹一句想到这种机制的大神真的应该膜拜!简直解放了生产力有没有!)OK,show you the code:

</>复制代码

  1. FragmentManager fragmentManager = getSupportFragmentManager();
  2. noteViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
  3. @Override
  4. public Fragment getItem(int position) {
  5. return null;
  6. }
  7. @Override
  8. public int getCount() {
  9. return notes.size();
  10. }
  11. });

是不是似曾相识?之所以要用FragmentPagerAdapter,是因为它可以为我们省去从Activity中启动Fragment的一系列事务代码,十分方便。写到这里我忽然忘记我们把笔记详情页给忘了,没有它我们该拿什么去显示笔记的详情呢,好吧,赶快去新建一个fragment命名为NoteDetailFragment:

</>复制代码

  1. public class NoteDetailFragment extends Fragment {
  2. private static String ARG_NOTE_ID;
  3. private Note note;
  4. TextView noteDate;
  5. TextView noteTitle;
  6. TextView noteContent;
  7. public NoteDetailFragment() {
  8. // Required empty public constructor
  9. }
  10. public static Fragment newFragment(UUID uuid){
  11. Bundle args = new Bundle();
  12. args.putSerializable(ARG_NOTE_ID,uuid);
  13. Fragment fragment = new NoteDetailFragment();
  14. fragment.setArguments(args);
  15. return fragment;
  16. }
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. UUID uuid = (UUID) getArguments().getSerializable(ARG_NOTE_ID);
  21. note = NoteLab.getNoteLab(getActivity()).getNote(uuid);
  22. }
  23. @Override
  24. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  25. Bundle savedInstanceState) {
  26. View root = inflater.inflate(R.layout.fragment_note_detail,container,false);
  27. noteDate = (TextView) root.findViewById(R.id.note_date);
  28. noteTitle = (TextView) root.findViewById(R.id.note_title);
  29. noteContent = (TextView) root.findViewById(R.id.note_content);
  30. noteDate.setText(note.getDate().toString());
  31. noteTitle.setText(note.getTitle());
  32. noteContent.setText(note.getContent());
  33. }
  34. return root;
  35. }

上面的newFragment方法和之前的newIntent所使用的技巧是一样的。从Activity中启动Fragment传参的方法是setArguments,自然的,我们就有了ARG_NOTE_ID这个常量。
对应的布局文件代码如下:

</>复制代码

</>复制代码

回到NotePagerActivity中:

</>复制代码

  1. @Override
  2. public Fragment getItem(int position) {
  3. Note note = notes.get(position);
  4. return NoteDetailFragment.newFragment(note.getUuid());
  5. }

好,编译,运行,添加几组测试数据后我们会发现不管从哪条笔记记录点击进去,都是从第一条开始显示,不用急,此时我们可以在getItem下面添加如下代码:

</>复制代码

  1. for (int i=0;i
  2. 用setCurrentItem来设置正确的笔记项就行了。因为结果需要动态演示,我就不贴图啦,如果有人需要代码的话我就联系我吧!我的qq:891871898求批评指正!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65562.html

相关文章

  • ViewPager2重大更新,支持offscreenPageLimit

    摘要:前言最近发布了版本,新增功能,该功能在上并不友好,现在官方将此功能延续下来,这回是骡子是马呢赶紧拉出来溜溜阅读指南内容基于版本讲解,由于正式版还未发布,如有功能变动有劳看官指出内容重点介绍的特性和预加载机制,另外包括的状态和的生命周前言 最近ViewPager2发布了1.0.0-alpha04版本,新增offscreenPageLimit功能,该功能在ViewPager上并不友好,现在官方将...

    番茄西红柿 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<