2
如果我有三個片段,我需要用另一個替換一個特定的片段,我該怎麼做?以及替換方法在片段事務中如何工作?我發現它用另一個替換一個片段,如果我有一個可見的片段,但如果我有兩個片段可見,它會替換一個隨機的片段,如果我有4個片段,它將替換兩個片段! 在這個程序下面我試圖通過使用(添加A)和(添加B)按鈕添加4片段,但問題存在,當我嘗試使用(用B代替A)或(用B代替B)按鈕時,這將取代隨機由片段A或片段B.如何用android中相同位置的另一個片段替換已存在的(特定)片段?
主要活動
package com.example.muhammad_adel.transaction;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
}
public void addA(View view) {
FragmentTransaction transaction = manager.beginTransaction();
FragmentA fragmentA = new FragmentA();
transaction.add(R.id.linearLayout, fragmentA, "addA");
transaction.commit();
}
public void removeA(View view) {
FragmentA fragmentA = (FragmentA) manager.findFragmentByTag("addA");
if (fragmentA != null && fragmentA.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(fragmentA);
transaction.commit();
}
}
public void replaceAwithB(View view) {
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayout, fragmentB, "addB");
transaction.commit();
}
public void addB(View view) {
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.linearLayout, fragmentB, "addB");
transaction.commit();
}
public void removeB(View view) {
FragmentB fragmentB = (FragmentB) manager.findFragmentByTag("addB");
if (fragmentB != null && fragmentB.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(fragmentB);
transaction.commit();
}
}
public void replaceBwithA(View view) {
FragmentA fragmentA = new FragmentA();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayout, fragmentA, "addA");
transaction.commit();
}
public void attachA(View view) {
FragmentA fragmentA = (FragmentA) manager.findFragmentByTag("addA");
if (fragmentA != null && !fragmentA.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.attach(fragmentA);
transaction.commit();
}
}
public void attachB(View view) {
FragmentB fragmentB = (FragmentB) manager.findFragmentByTag("addB");
if (fragmentB != null && !fragmentB.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.attach(fragmentB);
transaction.commit();
}
}
public void DetachedA(View view) {
FragmentA fragmentA = (FragmentA) manager.findFragmentByTag("addA");
if (fragmentA != null && fragmentA.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.detach(fragmentA);
transaction.commit();
}
}
public void DetachedB(View view) {
FragmentB fragmentB = (FragmentB) manager.findFragmentByTag("addB");
if (fragmentB != null && fragmentB.isVisible()) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.detach(fragmentB);
transaction.commit();
}
}}
public void replaceAOnlyWithB(View view) {
}
FragmentA
package com.example.muhammad_adel.transaction;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
FragmentB
package com.example.muhammad_adel.transaction;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.muhammad_adel.transaction.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add A"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:onClick="addA"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove A"
android:id="@+id/button2"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:textAllCaps="false"
android:onClick="removeA"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Replace A with B"
android:id="@+id/button3"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:textAllCaps="false"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout"
android:onClick="replaceAwithB"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attach A"
android:id="@+id/button4"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:onClick="attachA"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add B"
android:id="@+id/button5"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4"
android:textAllCaps="false"
android:onClick="addB"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove B"
android:id="@+id/button6"
android:layout_alignTop="@+id/button5"
android:layout_toRightOf="@+id/button5"
android:layout_toEndOf="@+id/button5"
android:textAllCaps="false"
android:onClick="removeB"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Replace B with A"
android:id="@+id/button7"
android:layout_alignBaseline="@+id/button6"
android:layout_alignBottom="@+id/button6"
android:layout_toRightOf="@+id/button6"
android:textAllCaps="false"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:onClick="replaceBwithA"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attach B"
android:id="@+id/button8"
android:layout_alignTop="@+id/button4"
android:layout_toRightOf="@+id/button5"
android:layout_toEndOf="@+id/button5"
android:textAllCaps="false"
android:onClick="attachB"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Replace A Only with B"
android:id="@+id/button9"
android:layout_below="@+id/button7"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:textAllCaps="false"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button4"
android:id="@+id/button11"
android:text="Detached A"
android:textAllCaps="false"
android:onClick="DetachedA"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button11"
android:layout_below="@id/button8"
android:text="Detached B"
android:textAllCaps="false"
android:onClick="DetachedB"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:layout_below="@+id/button11"></LinearLayout>
</RelativeLayout>
fragment_a.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"
android:background="#3498db"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello Man this is Fragment A"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFF"
/>
</RelativeLayout>
fragment_b.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"
android:background="#F92"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello man this is Fragment B"
android:id="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFF"
/>
</RelativeLayout>
對此表示抱歉,您不明白我的問題。 –
@MuhammadADel看到更新,並希望這可以幫助你 –
非常感謝,我試着說你的話,但這並不能回答我的問題。再次感謝您的嘗試。 –