android - 从 fragment 调用 fragment

标签 android android-fragments

我有一个带有按钮的 Activity ,当单击时,我用 fragment 上的另一个按钮调用 fragment 。但是当点击 fragment 的按钮时,我不能调用第二个 fragment 。这是我的来源,很简单: 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);
    }
}

我的代码有什么问题?

最佳答案

我认为现在可以使用 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 不是包含 subview 的好地方,因为它不是 ViewGroup,您可以从 xml 中删除 textView 并将其替换为 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" >

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

}

关于android - 从 fragment 调用 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627829/

相关文章:

android - 一个 Activity,多个 Fragments 和 setRetainInstance

java - 从父 Activity 访问 fragment 方法

android - onActivityResult 未在 Fragment 中调用

java - Java VM 上的 Android 错误。运行 Java SE 7

android - Gradle 突然不再构建 Arm8Release

java - 有没有办法重新启动应用程序,而不是让它在崩溃后重新启动最后一个 Activity ?

android - 来自服务器的带有数字标记的谷歌地图

android - 如何在单击按钮时清除 fragment 堆栈和 Activity 堆栈

android - 错误 INSTALL_FAILED_CONFLICTING_PROVIDER 我的应用程序没有任何提供商

android - RecyclerView 的内容不可见