在listitem中添加列表视图(Adding a list view inside a listitem)

编程入门 行业动态 更新时间:2024-10-28 01:21:01
在listitem中添加列表视图(Adding a list view inside a listitem)

我正在使用带有列表标题的列表片段,需要在第一个列表项中添加一组列表。

我使用自定义列表适配器来加载列表项。 现在对于列表项1 - 我需要在列表适配器中的列表项之上添加子视图。 我该怎么办呢? 我不确定可扩展列表项是否是我想在这里使用的。

我的自定义适配器的getView看起来像这样:

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE_LIST_ITEM_1: convertView = mInflater.inflate(R.id.list_item_one, null); /* add code */ /* add another list adapter? */ break; case TYPE_LIST_ITEM_OTHERS: convertView = mInflater.inflate(R.layout.list_item_other, null); /* add code for below list */ break; } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } return convertView; }

什么是最佳的实现方式,而不会丢失使用的标题视图

myListView.addHeaderView(myHeaderView);

I am using list fragment with a list header and need to add a set of list inside the first list item.

I am using custom list adapter to load the list items. Now for List Item 1 - I need to add a subview on top of list item from within the list adapter. How should I go about this? I am not sure if expandable list item is what I wanna use here.

My custom adapter's getView looks something like this:

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE_LIST_ITEM_1: convertView = mInflater.inflate(R.id.list_item_one, null); /* add code */ /* add another list adapter? */ break; case TYPE_LIST_ITEM_OTHERS: convertView = mInflater.inflate(R.layout.list_item_other, null); /* add code for below list */ break; } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } return convertView; }

What would be a optimal way of going about it, without losing the header view which uses

myListView.addHeaderView(myHeaderView);

最满意答案

如果没有重大错误,Android上的ScrollView中的ScrollView是不可能的,Google表示不这样做。 您可以在列表项中包含LinearLayout (其orientation设置为vertical ),并手动使用addView()将视图添加到将使用布局的设置方向显示的LinearLayout。

I figured out a work around by using the following code, putting it here:

When updating the custom list adapter (ex: myCustomListAdapter) using base adapter you can define different method to add list item and sublist items:

myCustomAdapter.addListItem(new ListItemObject (item)); myCustomAdapter.addSublistItem(new SublistItemObject (item));

And then use the getView to inflate different layouts and use your custom object to identify what you wanna do with them and how you want to display them. While the list adapter identify as all part of the list view.

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; int type = getItemViewType(position); final SuperListItemObject item = (SuperListItemObject) getItem(position); if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE_LIST_ITEM: convertView = mInflater.inflate(R.layout.list_item, null); holder.text=(TextView) convertView.findViewById(R.id.textview); break; case TYPE_SUBLIST_ITEM: convertView = mInflater.inflate(R.layout.sublist_item, null); holder.subtext=(TextView) convertView.findViewById(R.id.text); break; } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } if(item instanceof ListItemObject){ holder.text.setText("this is list item"); holder.text.setTextColor(Color.RED); //your code for list item } if(item instanceof SublistItemObject){ holder.subtext.setText("this is sublist item"); holder.subtext.setTextColor(Color.BLUE); //your code for sublist item } return convertView; }

Here the layout which you inflate for sublist can be customized using layout_marginLeft=20dp and layout_marginRight=20dp

更多推荐

本文发布于:2023-07-31 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   列表   listitem   view   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!