Android - ListView 中的 ListView 不会展开以显示所有项目
我有一个列表视图,其中包含作为子项的列表视图。我的问题是子列表视图不会展开以显示所有项目。
父布局
<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;
}
这是它正在做的事情。
我的目标是动态加载这些标头列表。该屏幕将是可滚动的,子列表始终显示每个项目。然而,无论我如何尝试,我似乎都无法让子列表完全扩展。我真的想避免用代码构建整个东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信使用 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)