新的PagerAdapter(getSupportFragmentManager()在导航抽屉片段中显示错误

时间:2015-10-19 08:04:20

标签: android android-fragments android-viewpager

我在viewpager的{​​{1}}中使用了fragment

错误是无法解析viewpager中的方法navigation drawer

在代码中:

'getSupportFragmentManager()'

我的整个代码是: GalleryFragment.java

final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

fragment_gallery.xml

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryFragment extends Fragment {


    private static GalleryFragment instance = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_gallery,container,false);

        return v;
        TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Image Gallery"));
        tabLayout.addTab(tabLayout.newTab().setText("Video Gallery"));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    public static GalleryFragment newInstance(String text) {

        if (instance == null) {
            // new instance
            instance = new GalleryFragment();

            // sets data to bundle
            Bundle bundle = new Bundle();
            bundle.putString("msg", text);

            // set data to fragment
            instance.setArguments(bundle);

            return instance;
        } else {

            return instance;
        }

    }

任何人都可以帮我解决我的错误..

1 个答案:

答案 0 :(得分:3)

您必须在getFragmentManager()中使用Fragment。如果Fragment来自支持库,则getFragmentManager()的返回值是受支持库中的值。