Android - ListView中的ListView不会展开以显示所有项目

时间:2011-06-06 17:23:35

标签: android android-layout android-listview android-arrayadapter

我有一个列表视图列表视图作为孩子。我的问题是子列表视图不会展开以显示所有项目。

父布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0px"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/site_wallet_header"
        style="@style/Home.ListHeader" />

    <ListView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/site_wallet_list" />


</LinearLayout>

儿童布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/ListItem"
    android:layout_gravity="center_vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/bank_icon"
        android:layout_height="wrap_content"
        android:layout_width="0dip"
        android:layout_weight="1" />

    <TextView

        android:id="@+id/bank_name"
        android:layout_height="wrap_content"
        android:layout_width="0dip"
        android:layout_weight="4"
        style="@style/ListItem.Text"/>

</LinearLayout>

父集ArrayAdapter

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(resourceId, null);
    }

    AccountSummary account = accounts.get(position);

    if (account != null) 
    {
        TextView header = (TextView) v.findViewById(R.id.site_wallet_header);
        header.setText(account.Name);

        if (account.Banks.length == 0) {
            //LinearLayout noBanks = (LinearLayout) context.getLayoutInflater().inflate(R.layout.no_banks, null);
        } else {
            ListView banksList = (ListView) v.findViewById(R.id.site_wallet_list);
            //banksList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

            banksList.clearChoices();
            banksList.setAdapter(new BankSummaryAdapter(context, R.layout.list_item_bank, account.Banks));
        }
    }

    return v;
}

儿童收藏ArrayAdapter

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(resourceId, null);
    }

    BankSummary bank = items.get(position);

    if (bank != null) 
    {
        TextView nameText = (TextView) v.findViewById(R.id.bank_name);
        ImageView iconImage = (ImageView) v.findViewById(R.id.bank_icon);

        nameText.setText(bank.DisplayName);

        Resources res = context.getResources();


        switch (bank.BankCode)
        {
            case 'V':
                iconImage.setImageDrawable(res.getDrawable(R.drawable.ic_visa));
                break;
            case 'M':
                iconImage.setImageDrawable(res.getDrawable(R.drawable.ic_mastercard));
                break;
            case 'D':
                iconImage.setImageDrawable(res.getDrawable(R.drawable.ic_discover));
                break;
            case 'A':
                iconImage.setImageDrawable(res.getDrawable(R.drawable.ic_american_express));
                break;
            default:
                iconImage.setImageDrawable(res.getDrawable(R.drawable.ic_echeck));
                break;
        }
    }

    return v;
}

这是它正在做的事情。

frustration

我的目标是让这些动态加载的标题列表。此屏幕将可滚动,子列表始终显示每个项目。然而,无论我尝试什么,我似乎都无法让子列表完全展开。我真的想避免在代码中构建整个东西。

1 个答案:

答案 0 :(得分:3)

我相信使用ExpandableListViewExpandableListAdapter代替这种复杂的布局,你会有更好的运气。

然后,您可以通过迭代它们并调用expandGroup(int groupPos)

来扩展每个组