2016-08-12 107 views
0

我想用FragmentA替換FragmentB。片段A由listView和FragmentB組成,其中包含一些TextView。在單擊列表視圖位置0處,FragmentB應替換FragmentA。在替換之前,MainActivity中的所有兩個片段都顯示正常。替換後FragmentA出現在FragmentB的底部。這意味着FragmentB內容不會消失,但它應該消失。被替換的片段無法刪除

FragmentA:

public class FragmentA extends ListFragment implements AdapterView.OnItemClickListener { 

    Get get; 
    public interface Get { 
     void getData(int s); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      get = (Get) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString()); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_a, container, false); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     ArrayList<String> arrayList = new ArrayList<>(); 
     for(int i = 0; i<10; i++) { 

      arrayList.add("a"); 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList); 
     setListAdapter(adapter); 
     getListView().setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
     get.getData(position); 
    } 
} 

FragmentB:

public class FragmentB extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 
} 

MainActivity:

public class MainActivity extends AppCompatActivity implements FragmentA.Get{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public void getData(int s) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

     if(s == 0) { 




FragmentA fragment = new FragmentA(); 
     fragmentTransaction.replace(R.id.fragment2, fragment); 
     fragmentTransaction.commit(); 
     Toast.makeText(this, String.valueOf(s), Toast.LENGTH_LONG).show(); 
     } 
    } 

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.zohaibsiddique.listfragment.MainActivity"> 

    <fragment 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:name="com.example.zohaibsiddique.listfragment.FragmentA" 
     android:id="@+id/fragment" /> 

    <fragment 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:name="com.example.zohaibsiddique.listfragment.FragmentB" 
     android:id="@+id/fragment2" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/fragment" 
     android:layout_toEndOf="@+id/fragment" /> 
</RelativeLayout> 

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

fragment_b.xml

package com.example.zohaibsiddique.listfragment; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.ListFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 

import java.util.ArrayList; 

/** 
* Created by Zohaib Siddique on 12/08/2016. 
*/ 
public class FragmentB extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 
} 
+0

可能是背景透明度導致此問題。 –

+0

而不是添加'fragmentTransaction.add(R.id.fragment2,fragment);'嘗試替換像'fragmentTransaction.replace(R.id.fragment2,fragment);' –

+0

什麼將是可能的解決方案? –

回答

0

的問題是在你的活動佈局。 FragmentB位於片段A的頂部。簡單的解決方案是在活動佈局中創建一個LinearLayout。像:

<?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.zohaibsiddique.listfragment.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@id/fragment_container"/> 

</RelativeLayout> 

然後在活動onCreate(),加上第一個片段:

FragmentA fragmentA = new FragmentA(); 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.commit(); 

然後創建添加FragmentB的方法,並在需要時調用此方法。

public void addFragmentB() { 
    FragmentB fragmentB = new FragmentB(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container, fragment); 
    fragmentTransaction.commit(); 
} 
+0

太棒了。我也找到了這個解決方案。 –

0

使用代替fragmentTransaction.add()

此代碼fragmentTransaction.replace()替換被添加到容器中的現有片段。這與爲所有當前添加的片段調用remove(Fragment)相同的containerViewId,然後使用此處給出的相同參數添加(int,Fragment,String)基本相同。

+0

已經試過但沒有工作 –

0
If(null!=fragment) 
    fragment=null; 
    switch(currentFragment){ 
    case A: 
    fragment = new FragmentA(); 
    break; 
    case B: 
    fragment= new FragmentB(); 
    break;} 
    fragmentTransaction.replace(R.id.frame_layout, fragment); 
    fragmentTransaction.commit(); 
    Toast.makeText(this, String.valueOf(s), Toast.LENGTH_LONG).show(); 

要做的修改:1。從您的activity_main中移除片段標籤並且具有單個幀佈局 2.在fragmentTransaction.replace中添加activity_main的framelayout的相應ID。

PS:你可以實現開關的情況下爲當前片段如圖所示,或者有自己的邏輯..

+0

它不起作用 –

+0

請看更新代碼 –

+0

檢查更新後的答案.. – Moulesh