问题描述
我的目的是像这样的列表中的一个列表:
My purpose is a list in a list like this:
-alllist--------
----row---------
----row---------
-----(list)-----
-------textview
-------textview
---row---------
-----(list)----
.....
MainAdapter
类可以与其他元素正常工作,但是PlaceAdapter
仅将第一个(或最后一个,我无法确定)项添加到嵌套"列表中.
MainAdapter
Class is working fine with other elements but PlaceAdapter
is adding only the first (or last, I couldn't figure) item to the 'nested' list.
出什么问题了?
MainAdapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PlaceHolder placeHolder = null;
if (convertView == null) {
placeHolder = new PlaceHolder();
convertView = mInflater.inflate(R.layout.convers_place, null);
placeHolder.listView = (ListView) convertView.findViewById(R.id.listView);
convertView.setTag(placeHolder);
} else {
placeHolder = (PlaceHolder)convertView.getTag();
}
PlaceAdapter adapter = new PlaceAdapter(ctx);
placeHolder.listView.setAdapter(adapter);
String arr[] = conversion.message.split(Pattern.quote("!!")); // Not empty
adapter.add(new PlaceModel(arr[0]));
adapter.add(new PlaceModel(arr[1]));
}
PlaceAdapter:
public class PlaceAdapter extends BaseAdapter {
Context ctx;
private List<PlaceModel> places = new ArrayList<>();
private LayoutInflater mInflater;
public PlaceAdapter(Context ctx){
this.ctx = ctx;
mInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void add(PlaceModel object) {
this.places.add(object);
}
@Override
public int getCount() {
return this.places.size();
}
@Override
public PlaceModel getItem(int position) {
return this.places.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PlaceHolder_ placeHolder = null;
PlaceModel place = getItem(position);
if (convertView == null) {
placeHolder = new PlaceHolder_();
convertView = mInflater.inflate(R.layout.convers_place_row, null);
placeHolder.name= (TextView)convertView.findViewById(R.id.row_name);
placeHolder.address= (TextView)convertView.findViewById(R.id.row_address);
placeHolder.map= (Button)convertView.findViewById(R.id.row_map);
convertView.setTag(placeHolder);
}
else {
placeHolder = (PlaceHolder_)convertView.getTag();
}
placeHolder.name.setText(place.getName());
placeHolder.address.setText(place.getAddress());
return convertView;
}
public static class PlaceHolder_ {
public TextView name;
public TextView address;
public Button map;
}
}
notifyDataSetChanged()
不受影响.
推荐答案
当我将ListView
放在ScrollView
内时,我也遇到类似的问题.
I've a similar problem when i put a ListView
inside a ScrollView
.
问题是:将我的ListView
设置为wrap_content
在可滚动视图内(在我的情况下为ScrollView
)只会使它跨越第一个孩子,而其他孩子将处于溢出状态.
The problem was: setting my ListView
to wrap_content
inside a scrollable view (ScrollView
in my case) will only make it span around the first child while the others would be in the overflow.
解决方法:更新适配器数据(例如添加新项目)后,我还从代码中更新了ListView
的高度(基于子级).
As workaround: after updating adapter data (e.g adding new item), i also update the ListView
's height (based on children) from code.
这是一个示例:
public class Utils {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
这篇关于Android嵌套listview适配器仅添加第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!