今天分享的内容较简单就是为了能发散思维,希望大神能给出其他实现的方式,给有用的人节约一点点时间。
listview中嵌套的listview只会显示被嵌套的listview只会显示第一行,所以要重谢设置listview的自定义adapter的高度。
知识点一:自定义adapter
知识点二:重写adapter的高度
实现listview嵌套listview的部分:
public class title extends BaseAdapter {
private RadioAdapter adapter;
private LayoutInflater inflater;
private String[][] authors;
private viewHolder holder;
// 标记用户当前选择的那一个作家
private int index = -1;
private Context c;
public title(Context c, String[][] authors) {
super();
this.c = c;
this.authors = authors;
inflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return authors.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.item_title, null);
holder.nameTxt = (TextView) convertView.findViewById(R.id.title);
holder.selectBtn = (ListView) convertView.findViewById(R.id.listViewRadio);
convertView.setTag(holder);
holder.nameTxt.setText(authors[position][0]);
adapter = new RadioAdapter(c ,authors[position]);
holder.selectBtn.setAdapter(adapter);
/*
* 重新定义listview中的adapter的高度
*/
setListViewHeightBasedOnChildren(holder.selectBtn);
return convertView;
}
public 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);
}
public class viewHolder {
public TextView nameTxt;
public ListView selectBtn;
}
}