【发布时间】:2014-05-18 13:11:58
【问题描述】:
我尝试使用具有自定义布局的 ListFragment:简单静态标题,在 SimpleCursorAdapter 提供的 ListContent 下方。列表本身也有自定义布局。 SimpleCursorAdapter 有效(查询很好,列表的自定义布局有效)。只要我不对 Fragment 本身使用自定义布局,一切正常。 如果我为 Fragment 添加布局,则只有标题(Textview)有效,列表保持为空。
列表的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/value"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right"
android:textSize="20sp" >
</TextView>
片段的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/last_update"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data">
</TextView>
我评论了一些东西 - 值得一试,因为我读到它有帮助 - 对我来说没有帮助。
最后一个机器人,尤其是片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewfragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TableDataGateway tableGate = new TableDataGateway(getActivity());
tableGate.UpdateUIRecords();
TextView tv = (TextView) getView().findViewById(R.id.last_update);
tv.setText(tableGate.getLastUpdateDate());
Cursor cursor = tableGate.selectUIRecords();
dBadapter = new SimpleCursorAdapter(getActivity(), R.layout.viewlayout, cursor, new String[] { DB_COL_GUI_LABEL, DB_COL_LAST_KNOWN_VALUE }, new int[] { R.id.label, R.id.value }, 0);
super.setListAdapter(dBadapter);
}
问题是:为什么 ListAdapter 没有连接到片段布局?
问候
毛毛虫
问题的澄清 我有类似的问题,如Android + ListFragment with a custom view hierarchy 但是我没有将内容堆叠在一起,我只在屏幕的上角看到 TextView 内容,而列表应该在下面的位置只是一个白色背景。 我很确定我已经按照 Google 的指示做了任何事情(http://developer.android.com/reference/android/app/ListFragment.html 关键字屏幕布局)
更新: 我不确定是否必须更改 main_activity.xml:
<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=".MainActivity" >
</RelativeLayout>
我还没有考虑过里面的fragment,不知道是否需要这个。昨天我尝试了一些功能来详细了解我的问题。我使用 getView().isShown() 并返回 FALSE - 这对我来说似乎是错误的,但我不知道这里有什么问题......
【问题讨论】:
-
你在
onCreate中定义了ListView吗?我没有看到这样的定义?您需要将您的适配器设置为列表视图和超级! -
你不能用默认的android名称覆盖id名称。
-
你能澄清你的意思吗?在 onActivityCreated 函数中,我执行
super.setListAdapter(dBadapter); -
据我了解,Fragments 没有 onCreate 方法,只有我已经返回自定义布局的 onCreateView 方法。 SetContentView 不适用于片段,至少我不知道我必须导入什么才能访问它。
-
您可以使用
getView()访问片段的运行活动。
标签: android android-layout android-fragments