研究了两天ViewPager,看了几篇网上的帖子,但总的来说看得一头雾水,理不清头绪,偶然发现了一篇简单易懂的帖子,讲的调理比较清晰,原文链接附在文后。
 
在本例中使用ViewPager + FragmentPagerAdapter + Fragment来实现页面的横向滑动,类似于手机中应用菜单左右滑动的效果。其中ViewPager用来加载页面,FragmentPagerAdapter为Fragment提供数据,而Fragment就是这里的”页面“,之所以用Fragment而没有使用View,是因为Fragment拥有更丰富的页面组件,可以实现更复杂的页面效果。
 
1、ViewPager布局文件
用于加载ViewPager组件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:gravity="center"
    android:src="@drawable/map"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</RelativeLayout>
2、在Activity中添加ViewPager和FragmentPagerAdapter 
其中getFragment()方法是用来向Fragment列表里添加内容并传递给MyPagerAdapter。
public class MainActivity extends AppCompatActivity {

    private MyPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Fragment> fragments = getFragments();
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
        ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
    }

    private List<Fragment> getFragments() {
        List<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.add(MyFragment.newInstance("Fragment 1"));
        fragmentList.add(MyFragment.newInstance("Fragment 2"));
        fragmentList.add(MyFragment.newInstance("Fragment 3"));
        return fragmentList;
    }
}
3、MyPagerAdapter类的具体实现
在这个类中主要存储了将要显示的Fragment列表,并重写了getItem()和getCount()两个方法。
public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}
4、创建MyFragment的布局文件
Fragment作为页面的载体需要有自己的布局文件,本例中在此布局中添加了一个TextView用来标示不同的页面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
5、MyFragment类的实现
在此类中通过Bundle将要显示的文本传递到MyFragment中来,并将其显示到TextView中去。MyFragment类中使用了静态类来避免重复创建。
public class MyFragment extends Fragment{
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

    public static final MyFragment newInstance(String message) {
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle(1);
        bundle.putString(EXTRA_MESSAGE, message);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
        String message = getArguments().getString(EXTRA_MESSAGE);
        View view = layoutInflater.inflate(R.layout.pager_layout, container, false);
        TextView textView = (TextView)view.findViewById(R.id.textView);
        textView.setText(message);

        return view;
    }
}

除了FragmentPagerAdapter类之外还有一些不同类型的Adapter可以使用,多了解这些类的内容对编写复杂应用有所帮助。本例中使用FragmentPagerAdapter和静态类来实现主要适用于页面相对固定的情况,如果要实现频繁动态改变页面,可以考虑继承FragmentStatePagerAdapter类来进行数据传递。

 

 

 

参考:

http://www.javacodegeeks.com/2013/04/android-tutorial-using-the-viewpager.html

posted on 2015-07-29 19:15  博客园博主  阅读(294)  评论(0编辑  收藏  举报