【问题标题】:How to use nested fragments with getChildFragmentManager()?如何通过 getChildFragmentManager() 使用嵌套片段?
【发布时间】:2023-04-08 12:21:01
【问题描述】:

编辑:大家好,我删除了所有内容,因为我终于找到了问题的答案(基本上如何在片段内的 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,因为scrollview 说我是个愚蠢的人并且不能正常工作。

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

【问题讨论】:

  • 发布你的日志
  • @sasikumar 你好,什么是 logcat,我在哪里可以找到它
  • 在android studio左下角点击android monitor然后你可以看到logcat
  • @sasikumar 谢谢我刚刚发布了它

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    改变这些方法

    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 所说的错误

    【讨论】:

    • 您好,不幸的是,这不起作用。这就是 logcat FATAL EXCEPTION: main Process: com.harrison.recipetest2, PID: 7718 java.lang.IllegalStateException: could not find method toFirst(View) in an parent or parents Context for android:onClick 属性在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener 查看 id 为“toFirst”的类 android.support.v7.widget.AppCompatButton。 onClick(AppCompatViewInflater.java:284)
    • 你能更新你写的Fragment类的最终代码吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多