0

我正在学习使用Hierarch Viewer 工具,但同时感谢任何关于为什么下面的布局没有正确呈现的提示。

我正在尝试呈现“html”表格视图。使用自定义列表适配器正确呈现内容(表体)。这行得通。但是,当我添加一个 textView(下面的 text_test)作为标题时,列表不再呈现。我可以看到通过我的适配器的代码流(填充视图持有者等),但在屏幕上我只看到标题文本,没有 listView。我曾经使用 FrameLayout(如评论中所述),但后来切换到 LinearLayout。

谢谢。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_list_view, container,
                false);
        ListView lv = (ListView)view.findViewById(R.id.my_list_view_one);
        // Tell the list view which view to display when the list is empty
        //lv.setEmptyView(getActivity().findViewById(R.id.my_list_view_empty));

        // Set up the adapter
        mCustomAdapter = new ListAdapterMyType(inflater, new ArrayList<MyType>());
        lv.setAdapter(mCustomAdapter);
        return  view;
    }

my_list_view.xml

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <!-- The frame layout is here since we will be showing either
the empty view or the list view.  -->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1">

        <TextView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="@string/text_test"/>

        <ListView
                android:id="@+id/my_list_view_one"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false"/>

        <!-- Here is the view to show if the list is emtpy -->
        <TextView android:id="@+id/my_list_view_empty"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="@string/list_no_items"
                  android:visibility="gone"/>

    </LinearLayout>
    </LinearLayout>
4

1 回答 1

1

But, when I add a textView (text_test below) to use as header, the list is not rendered anymore.

The TextView is set to match_parent in both the width and height, so it will fill the entire screen... To put a TextView above your ListView try:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="vertical">

    <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/text_test"/>

    <ListView
            android:id="@+id/my_list_view_one"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"/>

    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@+id/my_list_view_empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:text="@string/list_no_items"
              android:visibility="gone"/>

</LinearLayout>

I removed the outer LinearLayout, since it only had one child it did nothing. I also changed the remaining LinearLayout's orientation to vertical and set the header TextView height to wrap_content.

If you want your custom header to scroll with your ListView, use the addHeaderView() method (before calling setAdapter()) instead of a separate TextView in your layout. This approach will also hide the header when the ListView is empty.

于 2013-02-01T18:02:20.493 回答