2015-10-16 86 views
1

我有一個FragmentActivity,它應該將前屏幕片段(帶按鈕)替換爲點擊時按鈕指向的片段。片段不被替換

假設點擊調用SourceListFragment的按鈕。 onCreate()onCreateView()SourceListFragment被調用,但不知何故屏幕保持不變,即前屏幕片段沒有被替換。

我想知道我做錯了什麼。這是我的代碼。

public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener { 

... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 

    // set uncaught exception handler for thread 
    // Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); 

    setContentView(R.layout.activity_main); 

    FragmentManager fragmentManager = getSupportFragmentManager(); 

    mMainFrame = MainFragment.newInstance(); 
    fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit(); 
} 

    @Override 
    public void onFragmentInteraction(int id) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     switch (id) { 
      case R.id.button_sources: 
       Fragment fragment = new SourceListFragment(); 
       FragmentTransaction fragmentTrans = fragmentManager.beginTransaction(); 
       fragmentTrans.replace(R.id.main_screen, fragment); 
       fragmentTrans.addToBackStack(null); 
       fragmentTrans.commit(); 
       break; 
      default: 
       break; 
     } 
    } 
} 



public class SourceListFragment extends Fragment implements AbsListView.OnClickListener { 
    ... 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) { 

     Log.d("SOURCELIST", "SourceListFragment onCreateView\n"); 

     View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false); 

     return view; 
    } 


    public static SourceListFragment newInstance(BluetoothAdapter adapter) { 
     SourceListFragment fragment = new SourceListFragment(); 
     mBTAdapter = adapter; 
     return fragment; 
    } 

    // Container Activity must implement this interface 
    public interface OnFragmentInteractionListener { 
     public void onFragmentInteraction(int id); 
    } 
} 

這裏的佈局:

activity_main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:materialdesign="http://schemas.android.com/apk/res-auto" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#FFF" 
       android:orientation="vertical" 
       android:id="@+id/container"> 

</FrameLayout> 

fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/main_screen" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="148dp"> 

     <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="128dp" 
       android:background="#01579b" 
       android:paddingBottom="20dp" 
       android:paddingLeft="104dp" android:id="@+id/banner"> 
     </RelativeLayout> 

    </RelativeLayout> 

    <android.support.percent.PercentRelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     <Button 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/button_sources" 
       app:layout_widthPercent="30%" 
       app:layout_heightPercent="30%" 
       app:layout_marginTopPercent="10%" 
       app:layout_marginLeftPercent="15%" 
       app:layout_marginRightPercent="5%"/> 
... 

    </android.support.percent.PercentRelativeLayout> 
</LinearLayout> 

fragment_sourceitem_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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"> 

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

    <com.gc.materialdesign.views.ProgressBarCircularIndeterminate 
      android:id="@+id/progressBarCircularIndeterminate" 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:background="#1E88E5" /> 

</FrameLayout> 

任何幫助將不勝感激。

感謝

+0

你的意思是,用一個虛擬片段替換'fragment_sourceitem_list.xml'? –

+0

我試過一個空白的LinearLayout,但仍然一樣。 'onCreateView()'被調用 - 不是在渲染過程中調用onCreateView? –

+0

是的,沒錯。我已經添加了'MainActivity.onCreate()'來澄清事情。基本上id main_screen被添加到id容器中。 –

回答

1

您添加一個片段插入線性佈局ID main_screen,但片段應該添加到FrameLayout裏。在LinearLayout的情況下,轉換可以工作,但新添加的Fragment不在顯示中。

+0

我已經發現了這個問題。這是因爲我正在做'fragmentTrans.replace(R.id.main_screen,fragment);'而不是'fragmentTrans.replace(R.id.container,fragment);' –

+0

很高興你找到了問題所在。請接受我的回答,作爲幫助您找出問題的獎勵:) – ProblemSlover