码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment里面嵌套ViewPager

时间:2015-05-20 00:36:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:android   viewpager   fragment   

package net.mwplay.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import net.mwplay.adapter.HomeAdapter;
import net.mwplay.bean.TieBean;
import net.mwplay.bslm.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tian on 15/5/18.
 */
public class HomeFragment extends Fragment{

    @ViewInject(R.id.viewPager)
    ViewPager mViewPager;

    ViewPagerAdapter adapter;

    int NUMS = 5;

    String [] tabs = {"1", "2", "3", "4", "5"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.fragment_home, container, false);
       ViewUtils.inject(this, v);

       return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        adapter = new ViewPagerAdapter(getChildFragmentManager());//最关键的地方是这里
        mViewPager.setAdapter(adapter);

    }

    public class ViewPagerAdapter extends FragmentPagerAdapter{

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new TabFragment();
        }

        @Override
        public int getCount() {
            return NUMS;
        }
    }
}

最关键的地方在于传入 getChildFragmentManager

Fragment里面嵌套ViewPager

标签:android   viewpager   fragment   

原文地址:http://blog.csdn.net/qq634416025/article/details/45852163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!