MarcoN.
MarcoN.

Reputation: 61

How to scroll ExpandableListView in properly position when group is clicked

I have a ExpandableListView in my activity that works properly. I set in my adapter a clickListener on RelativeLayout, so when I click on it, my group get expanded. My group list is a ArrayList, my children is a HashMap> where Integer is groupPosition of ArrayList. Children is a comment list that any user can see. The user can add a comment by EditText at end of comment list. My goal is to position at top of screen the groupView clicked. I tried to do this but my problem is:

My first group has 10 comments. If i expand the first group I can see them and the position of my group clicked is right. If I try to expand my fourth group, when the first is already expanded, the ExpandableListView position on my screen goes to the fourth children of first group and not on fourth group just expanded. After if I scroll down the list the fourth group is open, but I would to position on the fourth group and not on fourth children of first group. All works properly if I open only group without comments...

small part of method getGroupView:

 scanCommFav=(RelativeLayout)v.findViewById(R.id.feedcell_scancommentfavorite);



scanCommFav.setOnClickListener(new OnClickListener(){

@Override 
                            public void onClick(View v)
                            {
                                if(!isExp)
                                {
                                    if(mess==0)
                                    {
                                        setIdx(idx);
                                        addIndex(idx);
                                        getCommentNoList();

                                    }
                                    else
                                    {
                                        callCommentScript(snapId);

                                        setIdx(idx);
                                        addIndex(idx);
                                    }

                                }
                                else if(isExp)
                                {
                                    setIdx(idx);
                                    removeIndex(idx);
                                    closeGroup(idx);


                                }
                            }
                        });

addIndex(idx) and removeIndex, add and remove in LinkedList the position of groups open. setIdx(idx) set to int variable the group position that I use for different purpose.

this is openGroup():

private void openGroup(int pos)
{
    feedlistView.setSelectedGroup(pos);
}

this is closeGroup():

private void closeGroup(int idx)
{
    feedlistView.collapseGroup(idx);
}

this is my listener:

    feedlistView.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,int position, long id) {
            if(ExpandableListView.getPackedPositionForGroup(position)==ExpandableListView.PACKED_POSITION_TYPE_GROUP)
            {
            int pos = feedlistView.getSelectedItemPosition();
            feedlistView.expandGroup(pos);
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });

I tried also to implement setOnGroupClickListener but doesn't works. Somebody has any idea? Thanks

Upvotes: 1

Views: 5004

Answers (1)

MarcoN.
MarcoN.

Reputation: 61

I solved saving Instance State and restoring Istance State every time I set my customer adapter to my Expandable ListView in this way:

Parcelable state = myExpList.onSaveIstanceState();
myExpList.setAdapter(myAdapter);
myExpList.onRestoreIstanceState();

after in my method openGroup(int position)

myExpList.expandGroup(position);
myExpList.setSelectedGroup(position);

I didn't use setOnItemSelectedListener.

Upvotes: 3

Related Questions