【问题标题】:Call fragment from fragment从片段调用片段
【发布时间】:2013-01-31 14:14:24
【问题描述】:

我有一个带有按钮的活动,当单击时,我用片段上的另一个按钮调用片段。但是当点击片段的按钮时,我不能调用第二个片段。这是我的来源,很简单: activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >

<Button 
    android:id="@+id/btn_click"
    android:text="Call Fragment"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:onClick="onClick"
    />
</LinearLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment1"
    android:text="Fragment 1"
    android:textSize="25sp"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

<Button 
    android:id="@+id/btn_frag2"
    android:text="Call Fragment 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment2"
    android:text="Fragment 2"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View v) {
    Fragment1 fragment1 = new Fragment1();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment1);
    fragmentTransaction.commit();
}

}

Fragment1.java

public class Fragment1 extends Fragment {

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

    public void onClick2(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fragment2);
        fragmentTransaction.commit();
    }
}

Fragment2.java

public class Fragment2 extends Fragment {

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

我的代码有什么问题?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    我认为现在可以使用 Fragment 嵌套,只需更新后面的可计算性 jar

    现在让我们深入研究它自己的问题。

    public void onClick2(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fragment2);
        fragmentTransaction.commit();
    }
    

    我认为R.id.fragment1 属于TextView,这不是包含子视图的好地方,因为它不是ViewGroup,您可以从xml 中删除textView 并将其替换为@ 987654326@ 可以说它会起作用,如果不是告诉我错误是什么。

    fragment1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    android:orientation="vertical" >
    
    <LinearLayout 
        android:id="@+id/fragment1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    
    <Button 
        android:id="@+id/btn_frag2"
        android:text="Call Fragment 2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    

    更新评论中的错误

    public class Fragment1 extends Fragment implements OnClickListener{
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment1, container, false);
    ((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this);
        return v;
    }
    
    public void onClick(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction =        fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, fragment2);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
    
    }
    

    【讨论】:

    • 它不工作。我用 LinearLayout 替换 TextView,第一个片段打开正常,但是当按下该片段上的按钮打开第二个片段时,应用程序停止并出现错误找不到活动类 com.example.fragments.MainActivity 中的方法 onClick2(View) id 为“btn_frag2”的视图类 android.widget.Button 上的 onClick 处理程序
    • 它正在工作,但我将 R.id.fragment1 替换为 android.R.id.content 并为返回堆栈添加 fragmentTransaction.addToBackStack(null);
    • @JackDaniel android.R.id.content 是什么,在哪里可以找到上述类的内容?
    • 此解决方案在合并@JackDaniel 建议的更改时有效。谢谢。
    【解决方案2】:

    如果你想用Fragment2 替换整个Fragment1,你需要在MainActivity 里面做,通过使用:

    Fragment2 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment2);
    fragmentTransaction.commit();
    

    只需将此代码放入MainActivity 的方法中,然后从Fragment1 调用该方法。

    【讨论】:

    • 这样,当我旋转手机时,我丢失了第二个片段,而第一个片段出现在屏幕上
    【解决方案3】:

    这是我的回答,它解决了同样的问题。 Fragment2.java 是 Fragment 类,它保存了 fragment2.xml 的布局。

    public void onClick(View v) {
                        Fragment  fragment2 = new Fragement2();
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.frame_container, fragment2);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();   
                    }
    

    【讨论】:

      【解决方案4】:

      我会使用监听器。 Fragment1 调用监听器(主要活动)

      【讨论】:

        【解决方案5】:

        此代码适用于我从 child_fragment 调用 parent_fragment 方法。

        ParentFragment parent = (ParentFragment) activity.getFragmentManager().findViewById(R.id.contaniner);

        parent.callMethod();

        【讨论】:

          【解决方案6】:

          在 MainActivity 中

          private static android.support.v4.app.FragmentManager fragmentManager;
          
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);                     
          
              fragmentManager = getSupportFragmentManager();                                                                 
          
          
              }
          
          public void secondFragment() {
          
              fragmentManager
                      .beginTransaction()
                      .setCustomAnimations(R.anim.right_enter, R.anim.left_out)
                      .replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null)
                      .commit();
          }
          

          在 FirstFragment 中调用 SecondFrgment 像这样:

          new MainActivity().secondFragment();
          

          【讨论】:

            【解决方案7】:

            这样做:getTabAt(标签的索引)

                    ActionBar actionBar = getSupportActionBar();
                    actionBar.selectTab(actionBar.getTabAt(0));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-05-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多