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



编辑:嘿伙计们,我已经删除了所有内容,因为我终于找到了问题的答案(基本上是如何在片段内的ViewPager中使用滚动视图(。这是我所有内容的代码。我是一个彻头彻尾的菜鸟,所以我真的不明白我的代码中发生了什么,但这里是:

这是我的寻呼机适配器,用于使片段进入ViewPager(我认为这就是发生的事情(我从CommonsGuy在线获取了此代码

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);
}
}

以下是RecipeFragmentxml文件和ViewPager中显示的不同fragments

<?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>

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

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecipeOverview">
<android.support.v4.widget.NestedScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Random Text"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Fragment1"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Random Text"
android:textStyle="bold"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some Button" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:text="Ingredients"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

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

更改这些方法

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所说,您会收到错误

最新更新