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);
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