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

发布于 2024-11-14 02:35:08 字数 4074 浏览 2 评论 0原文

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

父布局

<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

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

I have a listview of items that have a listview as a child. My problem is that the child listview does not expand to show all items.

Parent layout

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

Child layout

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

Parent collection 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;
}

Child collection 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;
}

Here is what it is doing.

frustration

My goal here is to have these header lists that are loaded dynamically. This screen will be scrollable with the child lists always showing every item. No matter what I try, however, I can't seem to get the child list to expand completely. I really want to avoid building the entire thing in code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-11-21 02:35:08

我相信使用 ExpandableListView 和 < a href="http://developer.android.com/reference/android/widget/ExpandableListAdapter.html" rel="nofollow">ExpandableListAdapter 而不是这种复杂的布局。

然后,您可以通过迭代每个组并调用 expandGroup(int groupPos) 来展开每个组

I believe you will have much better luck by using an ExpandableListView and an ExpandableListAdapter instead of this complicated layout.

You can then expand every group by iterating through them and calling expandGroup(int groupPos)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文