2013-08-03 48 views
0

問題是,當我從列表中單擊一個元素是不去詳細片段視圖..但它正常工作時,它進入風景模式。請告訴我我怎麼能保持縱向模式,從列表視圖轉到詳細視圖。Android片段不會去第二個片段

這是我的列表類

public class HadithList extends ListFragment 
    { 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onActivityCreated(savedInstanceState); 
      Log.d("ERROR", "In hadith list"); 
      String[] strHadith = new String[] {"Hadith one","Hadith two","Hadith three","Hadith four"}; 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity() 
        ,android.R.layout.simple_list_item_1,strHadith); 
      setListAdapter(adapter); 

     } 

     @Override 
     public void onListItemClick(ListView l, View v, int position, long id) { 
      // TODO Auto-generated method stub 
      String item = (String) getListAdapter().getItem(position); 
      HadithDetail hadithDetail = (HadithDetail) getFragmentManager().findFragmentById(R.id.hadith_detail); 
      //HadithDetail hadithDetail1 = (HadithDetail) getFragmentManager().findFragmentByTag("Details"); 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      Toast.makeText(getActivity(), "Selected "+position, Toast.LENGTH_SHORT).show(); 
      //if(hadithDetail != null && hadithDetail.isInLayout()) 
      hadithDetail.setText(getDetails(item)); 
      ft.replace(R.id.hadith_list, hadithDetail); 
      ft.commit(); 

     } 

     private String getDetails(String topic) 
     { 
      if(topic.toLowerCase().contains("one")) 
      { 
       return "Here is hadith 1 detail"; 
      } 
      if(topic.toLowerCase().contains("two")) 
      { 
       return "Here is hadith 2 detail"; 
      } 
      if(topic.toLowerCase().contains("three")) 
      { 
       return "Here is hadith 3 detail"; 
      } 
      if(topic.toLowerCase().contains("four")) 
      { 
       return "Here is hadith 4 detail"; 
      } 
      return "Cannot find detail"; 
     } 
    } 

這是我的詳細信息類

![public class HadithDetail extends Fragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     Log.d("ERROR", "In hadith detail"); 
     View view = inflater.inflate(R.layout.hadith_detail,container,false); 
     return view; 
    } 

    public void setText(String txt) 
    { 
     TextView view = (TextView) getView().findViewById(R.id.txtDetail); 
     view.setText(txt); 
    } 
}][1] 

活動主要

<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="horizontal" > 

    <fragment 
     android:id="@+id/hadith_list" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     class="com.example.hadith_app.HadithList" 
     /> 

</LinearLayout> 

細節佈局

<TextView 
     android:id="@+id/txtDetail" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|center_vertical" 
     android:layout_marginTop="20dip" 
     android:text="Hadith 1 Detail" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="30sp" 
     /> 


</LinearLayout> 

activity_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="horizontal" > 

    <fragment 
     android:id="@+id/hadith_list" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     class="com.example.hadith_app.HadithList" 
     /> 

    <fragment 
     android:id="@+id/hadith_detail" 
     android:layout_width="0dp" 
     android:layout_weight="2" 
     android:layout_height="match_parent" 
     class="com.example.hadith_app.HadithDetail" 
     /> 
</LinearLayout> 

回答

0

我相信你應該先閱讀這兩個環節!看到如何實現列表和詳細信息

http://www.vogella.com/articles/AndroidFragments/article.html

http://developer.android.com/guide/components/fragments.html

一些注意事項的例子。

1)首先,您應該在活動中提交您的交易。

2)Here ft.replace(R.id.hadith_list, hadithDetail);您正試圖用另一個替換您的靜態片段。它不這樣工作。 (我認爲你應該得到一個錯誤,但是我不確定)。

3)動態碎片應該添加在FrameLayout中。而不是與你的列表相同。

無論如何。只需檢查上面的鏈接,它可以很好地解釋你應該如何實現List和Details碎片,我相信你會發現什麼是錯的。

我無法提供一個完整的示例,因爲它肯定不會像您在上述教程中找到的示例那麼好。

GL

1

第一:FragmentTransaction.replace()花費不的片段的ViewGroup的ID。你需要在你的佈局XML中有一個ViewGroup(比如FrameLayout)作爲你的片段的容器。

第二:無法刪除在XML佈局中靜態聲明的片段。您需要在創建活動時以編程方式添加它。你可以這樣做:

public class MyActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState == null) { 
      // savedInstancState is null on the first time onCreate() runs 
      Fragment f = new HadithList(); 
      getFragmentManager().beginTransaction().add(R.id.fragment_container, f).commit(); 
     } 
    } 
} 
相關問題