It is bit tricky but i think the best possible solution for this problem is listView inside a listView.
Now let me explain,
There should be two list view
LV1 (ExtendedHeightListView) : It will have height equal to the height of all items (obviously view will not recycle in this listview). We will use it as a item for main list view.
public class ExtendedHeightListView extends ListView {
boolean expanded = false;
public ExtendedHeightListView(Context context) {
super(context);
}
public ExtendedHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
android.view.ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
LV2 (Default ListView) : Main ListView have items as extended height list view. (It is normal list view so your small list views will recycle).
Now considering you have data in grouped manner. Say
G1 - 2 items,
G2 - 5 items,
G3 - 1 item
BaseAdapter(BA1) for ExtendedHeightListView (LV1) :
public void BA1(Context context, Grp g) {
this.context = context;
this.grp = g;
}
public int getCount() {
return grp.getNumberOfItems();
}
public View getView(int position, ... ) {
// write here your normal getView;
}
BaseAdapter(BA2) for Main ListView (LV2) :
pass G1, G2 and G3 to the adapter.
public int getCount() {
return 3; //Number of groups
}
public View getView (int position, ... ) {
if (convertView == null) {
convertView = inflate LV1;
}
ExtendedHeightListView lv1 = convertView.findViewById(R.id.lv1);
Grp g = getGroup(position);
BA1 adapter = new BA1 (context, g);
//If background of each group is different
lv1.setBackgroundResource(R.drawable.bg);
lv1.setAdapter (adapter);
return convertView;
}
And set your OnItemClickListener on Main ListView (LV2).
I think it will help...