PageViewer嵌套在一个片段中

时间:2019-01-08 09:51:57

标签: java android android-fragments android-viewpager drawerlayout

我试图在片段中包含一个ViewPager,我已经看到我必须使用getChildFragmentManager(),但是我找不到使它工作的方法。 基本上,我所拥有的是抽屉式布局,具有一个侧面菜单,在这里我可以选择要使用的片段。在这些片段之一中,我需要一个带有2个子片段的ViewPager。 当我尝试使用子片段运行片段时,通常会收到错误消息“必须实现OnFragmentInteractionListener”。

我实际上拥有的代码,如果我将其作为活动运行,它会运行良好,但是由于片段化而使其崩溃

public class Calculator extends Fragment implements Basic.OnFragmentInteractionListener, Cientific.OnFragmentInteractionListener{

private SectionsPagerAdapter mSectionsPagerAdapter;
private OnFragmentInteractionListener mListener;
private ViewPager mViewPager;

public Calculator() {
    // Required empty public constructor
}
public static Calculator newInstance(String param1, String param2) {
    Calculator fragment = new Calculator();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

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

    // Set up the ViewPager with the sections adapter.

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        mViewPager = (ViewPager) getView().findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        //here i already tried using getChildFragmentManager() 
        // but i dont know if i used it wrong
    }


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_calculator, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {

    void onFragmentInteraction(Uri uri);
}

@Override
public void onFragmentInteraction(Uri uri) {

}


public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }


    public static Fragment newInstance(int sectionNumber) {
        Fragment fragment=null;

        switch (sectionNumber){
            case 1: fragment=new Basic();
                break;
            case 2: fragment=new Cientific();
                break;
        }

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.content_main, container, false);
        return rootView;
    }
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }
}

1 个答案:

答案 0 :(得分:0)

context中收到的onAttach(Context context)Activity的实例,因此要使其正常工作,您需要在OnFragmentInteractionListener中实现Activity