2016-11-19 35 views
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> 

回答

0

您可以通过以下方法更改片段 第1步:设置您要替换的布局的ID与其他片段。

片段XML

<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" 
tools:context="com.example.neelay.level47.Fragment.Album" 
android:background="#4d263238" 
android:id="@+id/replace_frag"> 

<!-- TODO: Update blank fragment layout --> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button" /> 

第2步:设置在片段的onclick,只是做的片段交易

片段的Java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_album, container, false); 

    Button button =(Button)view.findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Fragment someFragment = new Replacement(); 
      FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
      transaction.replace(R.id.replace_frag, someFragment); // give your fragment container id in first parameter 
      transaction.addToBackStack(null); // if written, this transaction will be added to backstack 
      transaction.commit(); 
     } 
    }); 
    return view; 
} 

我正在更换可滚动选项卡中的片段,如果您要替换所有可滚动选项卡,则可以设置您的content_main的编号

+0

对此表示抱歉,您不明白我的问题。 –

+0

@MuhammadADel看到更新,并希望这可以帮助你 –

+0

非常感谢,我试着说你的话,但这并不能回答我的问题。再次感谢您的尝试。 –

相关问题