«

Fragment里面嵌套ViewPager

时间:2024-3-2 17:43     作者:韩俊     分类: Android


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

标签: android

热门推荐