Reputation: 2499
EDIT
I found solution, please see below.
ORIGINAL:
I created an ExpandableListView
and it looks like this :
Each Group view has 3~5 child view, and I force to expand all group view,
mExpandableListView = (ExpandableListView) mView.findViewById(R.id.expandable_list);
CollectionExpandableListAdapter adapter = new MyExpandableListAdapter(getActivity(), mGroups, mProdChild);
adapter.setInflater(LayoutInflater.from(getActivity().getBaseContext()), getActivity());
mExpandableListView.setAdapter(adapter);
mExpandableListView.setGroupIndicator(null);
for (int i = 0; i < groupList.size() - 1; i++) {
mExpandableListView.expandGroup(i);
}
Also, here's my adapter code :
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<String> mGroupItem;
private ArrayList<ProductListData> mTempChild = new ArrayList<ProductListData>();
private ArrayList<Object> mChildItem = new ArrayList<Object>();
private LayoutInflater mInflater;
private Activity mActivity;
private ImageLoader mImageLoader;
public CollectionExpandableListAdapter(Activity context, ArrayList<String> grList,
ArrayList<Object> childItem) {
mGroupItem = grList;
mChildItem = childItem;
mActivity = context;
mImageLoader = new ImageLoader(mActivity);
}
public void setInflater(LayoutInflater inflater, Activity activity) {
mInflater = inflater;
mActivity = activity;
}
@Override
public ProductListData getChild(int groupPosition, int childPosition) {
return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
mTempChild = (ArrayList<ProductListData>) mChildItem.get(groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapter_collection_row_child, null);
}
...
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroupItem.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroupItem.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup arg3) {
Log.d("LOG", "Group position : " + groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_row_layout, null);
}
...
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
Creating an expandableListView is working fine, but I keep getting 0 value in getGroupView()
when I scroll the list. In my logcat, I was expecting to see groupPosition
as 0, 1, 2, 3, 4..
(when I scroll down), but it actually shows 0, 0, 0, 1, 0, 2, 0, 3, 0, 4..
which has additional 0
before each groupPosition
. This happens only at the first scroll, so if I scroll up and down several times, it doesn't show additional 0 value.
I googled it and spent a lot of time to debugging it, but I can't find any solutions. Can anyone help me out?
Solution
My problem was in the Layout of Listview
. I changed layout_height
to match_parent
, and the bug disappeared! I found the solution here.
Upvotes: 4
Views: 2515
Reputation:
I had a similar problem once, getting the wrong views. Not sure if this is the case here but what I did was to comment/delete the line:
if (convertView == null) {
in both the getGroupView method and the getChildView method in the listadapter.
and ofc the corresponding end curly brackets.
It has something to do with the listadapter reusing views for using less systemresources when displaying the views, but it makes it harder to manage imo.
Upvotes: 4