简体   繁体   中英

ViewPager inside a Fragment (NestedFragments)

My app has the main activity with a Navigation Drawer, that allow the application to navigate between Fragments. Each item list of the navigation drawer shows a fragment.

In one of those Fragment I want a ViewPager, and here is where I got stuck. If I'm not wrong, this is a Fragment inside another Fragment (Nested Fragments) but I can't make this work.

This is the code of the fragment that contains the ViewPager Seccion1.java

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class Seccion1 extends Fragment {

    ViewPager vp;

    private List<android.support.v4.app.Fragment> getFragments(){
        List<android.support.v4.app.Fragment> list = new ArrayList<android.support.v4.app.Fragment>();
        list.add(ModelFragment.newInstance("Hola como andas", "descriociones"));
        list.add(ModelFragment.newInstance("0895452125x", "#FFFFEF59"));
        list.add(ModelFragment.newInstance("22 Mart", "#FF9C63FF"));
        list.add(ModelFragment.newInstance("Sak", "#72a83b"));
        list.add(ModelFragment.newInstance("Android Studio", "#a83b97"));
        return list;
    }

    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.seccion1, container, false);

        vp = (ViewPager)rootView.findViewById(R.id.vp);

        //vp.setPageTransformer(true, new RotateDownTransformer());
        ControllerFragment cf = new ControllerFragment(getChildFragmentManager(), getFragments());
        vp.setAdapter(cf);

        return rootView;
    }
}

I don't know how to fix this line:

ControllerFragment cf = new ControllerFragment(getChildFragmentManager(), getFragments());

图片错误

Here the ControllerFragment code ControllerFragment.java

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;

public class ControllerFragment extends FragmentPagerAdapter {
    List<Fragment> fragments;
    Context context;

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

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

    public int getCount(){
        return fragments.size();
    }

    public Object instantiateItem(View view, int position){
        RelativeLayout relativeLayout = (RelativeLayout)view.inflate(context, R.layout.custom_fragment, null);
        TextView tv_text = (TextView)relativeLayout.findViewById(R.id.tv_text);
        TextView tv_description = (TextView)relativeLayout.findViewById(R.id.tv_description);
        ((ViewPager) view).addView(relativeLayout);
        return relativeLayout;
    }
}

Change this

import android.app.Fragment;

to

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM