android ViewPager嵌套Fragment实现侧滑翻页

ViewPager嵌套Fragment实现侧滑翻页

PagerAdapter有两个子类:FragmentPagerAdapter和FragmentStatePagerAdapter,他们都是专门用来给支持包中出现的ViewPager进行数据适配的。FragmentPagerAdapter拥有自己的缓存策略,当和ViewPager配合使用的时候,会缓存当前Fragment以及左边一个、右边一个,一共三个Fragment对象。FragmentStatePagerAdapter是PagerAdapter的子类,这个适配器对实现多个Fragment界面的滑动是非常有用的,它的工作方式和listview是非常相似的。当Fragment对用户不可见的时候,整个Fragment会被销毁,只会保存Fragment的保存状态。基于这样的特性,FragmentStatePagerAdapter比FragmentPagerAdapter更适合用于很多界面之间的转换,而且消耗更少的内存资源。这里使用FragmentStatePagerAdapter来实现10个页面之间的侧滑切换。买个页面加载一个ListFragment。并且在视图底部提供两个按钮,一个跳到首页,一个跳到最后一页。

  效果图:

这里写图片描述

  为了贴代码简单,我把所有的类,包括:一个FragmentActivity的子类,一个ListFragment的子类,以及一个FragmentStatePagerAdapter的子类都写在同一个class文件中,源码如下:
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
    private ViewPager viewPager;
    private static final int NUMBER = 10; // 定义页数
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        // 添加按键监听事件
        this.findViewById(R.id.first).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewPager.setCurrentItem(0);// 点击按钮,跳到第一页
            }
        });
        // 添加按键监听事件
        findViewById(R.id.last).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewPager.setCurrentItem(NUMBER - 1); // 点击按钮, 跳到最后一页
            }
        });
        viewPager.setAdapter(new myAdapter(getSupportFragmentManager()));
    }

    // 给ViewPager自定义一个适配器
    public static class myAdapter extends FragmentStatePagerAdapter {

        public myAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return ArrayListFragment.getFragment(arg0);// 获取一个Fragment并且返回
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return NUMBER;// 返回长度、个数
        }

    }

    // 定义一个Fragment
    public static class ArrayListFragment extends ListFragment {
        private TextView textView;
        int num;

        public static ArrayListFragment getFragment(int num) {// 定义一个方法,返回一个Fragment并且传递一个参数
            ArrayListFragment arrayListFragment = new ArrayListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("num", num);
            arrayListFragment.setArguments(bundle);// 传递参数
            return arrayListFragment;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {// 启动时在调用onCreate后调用
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_expandable_list_item_1, getData()));// 给ListFragment创建一个适配器
        }

        public List<String> getData() {// 给ListFragment提供一组数据
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < 20; i++) {
                list.add("第 -" + i + "- 行");
            }
            return list;
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
            // ListFragment点击监听事件
            Toast.makeText(getActivity(), "正在点击第" + num + "页,第" + id + "行", 1)
                    .show();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            // 获取Fragment传递过来的值,如果为空则赋值1;
            num = (getArguments() != null ? getArguments().getInt("num") : 1);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            // 给Fragment加载视图
            View view = inflater.inflate(R.layout.fragment_layout, null);
            textView = (TextView) view.findViewById(R.id.fragment_textView1);
            textView.setText("第 -" + num + "- 页");
            return view;
        }

        @Override
        public void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
        }
    }
}

两个布局文件,一个给Activity,一个给ListFragment。

Activity布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="9"
        android:orientation="vertical" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/first"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="跳到第一页" />

        <Button
            android:id="@+id/last"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="跳到最后一页" />
    </LinearLayout>

</LinearLayout>

ListFragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/fragment_textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="7"
        android:orientation="vertical" >

        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>

注意:ListFragment中的ListView ID必须定义为 android:id=”@id/android:list”,在程序中使用setlistAdapter加载。

发布了34 篇原创文章 · 获赞 10 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/q296264785/article/details/53316772
今日推荐