...欢呼声 (编辑)的 我查了很多类似这样的帖子,但我不能解决它
场景: 2片段=片段a和b 1个按钮(使用onclick方法)
我试图做的事情: 使用带按钮的onlick方法从片段a调用片段b
尝试在这里浏览loooooooottttt的帖子,但仍然不明白如何正确地做到这一点,尤其是关于从xml更改“容器”的部分 (工作)
它的工作但现在我很困惑为什么当我点击按钮时片段落在彼此上 请帮助我,我在极限
第一个片段:
package com.example.fabio.tabdrawer;
import....
public class Menu_Berita extends Fragment {
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
if (container == null) {
return null;
}
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
//this fragment part which i changed above
final Button button_next = (Button) mRelative.findViewById(R.id.button_next);
button_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, new Menu_riwayat()).commit();
//still not working properly, error in logcat
}
});
return mRelative;
}}
第一个片段的xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="test1"
android:id="@+id/text_test1"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_test1"
android:entries="@array/spinner_resiko_string"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Resiko"
android:id="@+id/text_test2"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_test2"
android:entries="@array/spinner_syariah_string"
android:layout_marginLeft="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:id="@+id/button_next"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
</FrameLayout>
第二个片段:
public class Menu_Riwayat extends Fragment {
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
if (container == null) {
return null;
}
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.riwayat_layout, container, false);
return Relative;
}}
第二个片段的xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_horizontal">
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="129dp" />
</FrameLayout>
(已删除logcat)
screnshot: fragment fall on each other