Java 安卓碎片相互碰撞
干杯。。。 (编辑) 我查了很多类似的帖子,但我都查不出来 情景: 2片段=片段a和b 1个按钮(使用onclick方法) 我想做的是: 使用带有按钮的onlick方法从片段a调用片段b 尝试在这里浏览post的looootttt,但仍然不理解如何正确地执行,特别是关于从xml更改“container”的部分 (工作) 它可以工作,但现在我不明白为什么当我点击按钮时碎片会互相掉落 请帮帮我,我已经到了极限 第一段:Java 安卓碎片相互碰撞,java,android,xml,android-fragments,onclicklistener,Java,Android,Xml,Android Fragments,Onclicklistener,干杯。。。 (编辑) 我查了很多类似的帖子,但我都查不出来 情景: 2片段=片段a和b 1个按钮(使用onclick方法) 我想做的是: 使用带有按钮的onlick方法从片段a调用片段b 尝试在这里浏览post的looootttt,但仍然不理解如何正确地执行,特别是关于从xml更改“container”的部分 (工作) 它可以工作,但现在我不明白为什么当我点击按钮时碎片会互相掉落 请帮帮我,我已经到了极限 第一段: package com.example.fabio.tabdrawer; imp
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;
}}
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;
}}
<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>
第一个片段的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>
第二个片段的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;
}}
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;
}}
<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已移除)
镜头:
您应该使用new Menu_riwayat()而不是Menu_riwayat…,这将为您提供Menu_riwayat类型片段的新实例设置适当的容器以替换视图,并遵循阴影机器人的指示。另外,这里处理的是嵌套片段。您正在替换片段中的片段,在活动中托管scrollview并使用广播接收器或事件(EventBus)从片段a调用活动的replaceFragment()会更简单。@ShadowDroid我将其更改为new Menu_riwayat(),我得到了IllegalStateException错误,系统声明ScrollView只能承载一个直接的孩子。我试图用FrameLayout包装ScrollView,它可以工作,但结果就像两个片段互相掉落一样。知道怎么修吗?(我已经编辑了这篇文章)