Android - ListView within a ListView does not expand to show all items
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.
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.
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)
精彩评论