ExpandableListView.addHeaderView() 没有保留布局参数

ExpandableListView.addHeaderView() did not retain layout parameter

我有一个 RelativeLayouts,我想将其作为 header 添加到 ExpandableList。基本上,布局与我用于列表中 child 视图的布局相同。

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:paddingLeft="50dp"
    android:paddingStart="50dp"
    android:paddingRight="16dp"
    android:paddingEnd="16dp">

    <TextView
        android:id="@+id/category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Electricity"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>

    <TextView
        android:id="@+id/current_balance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="Rp 300.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>

    <TextView
        android:id="@+id/current_budget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Rp800.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/secondary_text"/>

    <TextView
        android:id="@+id/current_expenses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:text="Rp800.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/secondary_text"/>

</RelativeLayout>

然后我在onCreateView里面添加header如下:

View view = inflater.inflate(R.layout.fragment_budget_list, container, false);

View listHeader = inflater.inflate(R.layout.row_budget_item, null);
// populate views in the header

ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.list_budget);
listView.addHeaderView(listHeader);
listView.setAdapter(mAdapter);

显示了 header,但是,看起来高度已更改为 0,因为第一行中的 TextView(category_name 和 current_balance)与第 2 行(current_budget 和 current_expenses)。

(注意:用作列表的 child 视图时布局显示正确)

这是因为布局转换为AbsListView? 如何保留布局参数? 我试过 listHeader.setMinimumHeight() 但没用

我在您的布局中没有看到列表视图。 如果你想在列表中添加 header,我建议你这样添加:

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

   <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/activity_chat_listView"
    android:layout_above="@+id/linearLayout"
    android:stackFromBottom="true"
    android:paddingBottom="3dp"
    android:transcriptMode="alwaysScroll"/>
</RelativeLayout>

然后在 onCreate 添加 mListView.setAdapter(mChatAdapter); where private ChatAdapter mChatAdapter; 我假设您提供的这个布局将通过 Loader<Cursor> 加载多次 现在,在 ChatAdapter bindView 或 GetView 中,您将决定何时希望此 header 可见或消失。

有很多 Cursor 适配器的好例子可以看。 在这里检查一个 https://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/ 这取决于您的需要。 希望对你有帮助

从这个 post

中找到了我的答案

显然,在扩展页眉布局时,我需要提供 ExpandableListView 作为根和 falseattachToRoot。因此,布局参数值将提供给根视图。所以代码看起来像这样:

View view = inflater.inflate(R.layout.fragment_budget_list, container, false);
ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.list_budget);

View listHeader = inflater.inflate(R.layout.row_budget_item, listView, false);

来自docs

root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)