Ich habe 3 Fragment und 2 framelayout in 1 XML-Datei hier, das erste 2 Fragment ist ein Text und es wird normalerweise angezeigt, aber wenn ich das dritte Fragment, die seekbar haben, ersetzt haben dann verschwand es! Aber wenn ich das dritte Fragment zuerst zeige, dann ging die Suchleiste normal und die TextView ging weg! Dies ist mein Code:SeekBar verschwinden, wenn Fragment ändern?
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
fragment1= new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment1,R.id.fragment1,"FRG1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment2,R.id.fragment2,"FRG2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment3,R.id.fragment2,"FRG3");
}
});
}
public void replaceFragment(Fragment fragment , int containerResId, String tag){
android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager()
.beginTransaction();
trans.replace(containerResId , fragment , tag);
trans.commit();
getSupportFragmentManager().executePendingTransactions();
}
Fragment 1:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}
Fragment 2 und 3 ist die gleiche wie fragment1.
Dies ist 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="vertical"
tools:context="com.example.macbook.fragment.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment3"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment1"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="@+id/fragment2"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
</LinearLayout>
Fragment3.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
and this happen when i display fragment 3 which have seekbar
if i display fragment 3 first then any TextView can't display
replaceFragment (Fragment3, R.id.fragment2, "FRG3"); zu replaceFragment wechseln (Fragment3, R.id.fragment1, "FRG3"); und kommentieren Sie das Fram-Layout – Pavya
Können Sie diese Situation testen: FrameLayout (von Fragment 1 und 2) Höhe auf statische Größe (ex: 200dp) setzen, layout_weight entfernen und sehen, was passiert. –