问题是当我从列表中单击一个元素时没有进入细节片段视图..但是当它进入横向模式时它工作正常。请告诉我如何从列表视图转到详细视图保持纵向模式。
这是我的列表类
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 (Land * 强文 *)
<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 :(得分:1)
首先:FragmentTransaction.replace()
获取ViewGroup的ID,而不是片段的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();
}
}
}
答案 1 :(得分:0)
我相信你应该先阅读这两个链接!查看如何实现列表和详细信息的示例
http://www.vogella.com/articles/AndroidFragments/article.html
http://developer.android.com/guide/components/fragments.html
一些注意事项:
1)首先,您应该在活动中提交您的交易。
2)这里ft.replace(R.id.hadith_list, hadithDetail);
你试图用另一个静态片段替换你的静态片段。它并不像这样工作。
(我认为你这样做时会出错,但我不确定)。
3)应在FrameLayout中添加动态片段。而不是与列表相同的布局。
无论如何,只需查看上面的链接,这些链接很好地解释了如何实现List和Details Fragments,我相信你会发现错误。
我无法提供完整的示例,因为它肯定不如您在上述教程中找到的示例一样好。