如何使用嵌套片段与getChildFragmentManager()?

时间:2017-07-06 05:12:58

标签: android android-fragments android-viewpager android-scrollview android-nested-fragment

编辑:嘿伙计们我删除了所有内容,因为我终于找到了我的问题的答案(基本上如何在片段中使用ViewPager内部的scrollview)。这是我的一切代码。我是一个总菜鸟,所以我真的不明白我的代码中发生了什么,但现在是:

这是我的寻呼机适配器,让片段进入ViewPager (我认为这就是发生的事情)我从CommonsGuy p>

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;
import java.util.List;


public class PagerAdapter extends FragmentPagerAdapter {

  private List < Fragment > fragments;


  public PagerAdapter(FragmentManager fm, List < Fragment > fragments) {
    super(fm);

    this.fragments = fragments;
  }

  @Override
  public Fragment getItem(int i) {
    return fragments.get(i);
  }

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

以下是我将ViewPager放在

上的片段代码

public class RecipeFragment extends Fragment {


  public RecipeFragment() {
    // Required empty public constructor
  }


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


    List < Fragment > fragments = new Vector < > ();
    fragments.add(Fragment.instantiate(getActivity(), RecipeOverview.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), RecipeIngredients.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), RecipeProcedures.class.getName()));
    PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), fragments);
    ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
    pager.setAdapter(adapter);


    return (view);
  }

}

以下是xml的{​​{1}}个文件和RecipeFragment分别显示的fragments

ViewPager

ViewPager中迷你片段的布局。而.java迷你片段文件只是一个普通的片段文件。我也使用了nestedScrollView,因为scrollview说我是愚蠢的人并且无法正常工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

2 个答案:

答案 0 :(得分:0)

您在片段中定义的OnClick方法(toFirst,toSecond)需要在您的Activity中定义(可能是MainActivity)。

答案 1 :(得分:0)

更改这些方法

public void toFirst(){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page1 fragment = new Fragment1Page1();
        ft.replace(R.id.contentLayoutFragment1Page1, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
    public void toSecond(){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page2 fragment = new Fragment1Page2();
        ft.replace(R.id.contentLayoutFragment1Page2, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }

public void toFirst(View view){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page1 fragment = new Fragment1Page1();
        ft.replace(R.id.contentLayoutFragment1Page1, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
    public void toSecond(View view){
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Fragment1Page2 fragment = new Fragment1Page2();
        ft.replace(R.id.contentLayoutFragment1Page2, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }

当你在xml中提供这个方法时,这会将视图目标传递给那些方法,因为你的代码中缺少你正在收到错误,因为logcat说