2013-08-03 46 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(); 
     } 
    } 
} 
相关问题