Change expandable indicator in ExpandableListView

前端 未结 7 802
野趣味
野趣味 2020-11-29 18:51

Trying to create an ExpandableListView. The initial view with the groups shows up fine. However, when I click the list item, my arrow does not change. See the images below.<

7条回答
  •  甜味超标
    2020-11-29 19:46

    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.Context;
    import android.database.DataSetObserver;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.TextView;
    
    public class MyActivity extends Activity {
    
        private ExpandableListView mExpandableList;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
    
            mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
            mExpandableList.setGroupIndicator(null);
            ArrayList arrayParents = new ArrayList();
            ArrayList arrayChildren = new ArrayList();
    
            //here we set the parents and the children
            for (int i = 0; i < 10; i++){
                //for each "i" create a new Parent object to set the title and the children
                Parent parent = new Parent();
                parent.setTitle("Parent " + i);
    
                arrayChildren = new ArrayList();
                for (int j = 0; j < 10; j++) {
                    arrayChildren.add("Child " + j);
                }
                parent.setArrayChildren(arrayChildren);
    
                //in this array we add the Parent object. We will use the arrayParents at the setAdapter
                arrayParents.add(parent);
            }
    
            //sets the adapter that provides data to the list.
            mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
    
        }
    
        public class Parent {
            private String mTitle;
            private ArrayList mArrayChildren;
    
            public String getTitle() {
                return mTitle;
            }
    
            public void setTitle(String mTitle) {
                this.mTitle = mTitle;
            }
    
            public ArrayList getArrayChildren() {
                return mArrayChildren;
            }
    
            public void setArrayChildren(ArrayList mArrayChildren) {
                this.mArrayChildren = mArrayChildren;
            }
        }
    
    
    
    
        public class MyCustomAdapter extends BaseExpandableListAdapter implements OnClickListener{
    
    
            private LayoutInflater inflater;
            private ArrayList mParent;
    
            public MyCustomAdapter(Context context, ArrayList parent){
                mParent = parent;
                inflater = LayoutInflater.from(context);
            }
    
    
            @Override
            //counts the number of group/parent items so the list knows how many times calls getGroupView() method
            public int getGroupCount() {
                return mParent.size();
            }
    
            @Override
            //counts the number of children items so the list knows how many times calls getChildView() method
            public int getChildrenCount(int i) {
                return mParent.get(i).getArrayChildren().size();
            }
    
            @Override
            //gets the title of each parent/group
            public Object getGroup(int i) {
                return mParent.get(i).getTitle();
            }
    
            @Override
            //gets the name of each item
            public Object getChild(int i, int i1) {
                return mParent.get(i).getArrayChildren().get(i1);
            }
    
            @Override
            public long getGroupId(int i) {
                return i;
            }
    
            @Override
            public long getChildId(int i, int i1) {
                return i1;
            }
    
            @Override
            public boolean hasStableIds() {
                return true;
            }
    
            @Override
            //in this method you must set the text to see the parent/group on the list
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    
                if (view == null) {
                    view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
                }
                view.findViewById(R.id.button).setTag(i);
                view.findViewById(R.id.button).setOnClickListener(this);
                TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
                //"i" is the position of the parent/group in the list
                textView.setText(getGroup(i).toString());
    
                //return the entire view
                return view;
            }
    
            @Override
            //in this method you must set the text to see the children on the list
            public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
                }
    
                TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
                //"i" is the position of the parent/group in the list and
                //"i1" is the position of the child
                textView.setText(mParent.get(i).getArrayChildren().get(i1));
    
                //return the entire view
                return view;
            }
    
            @Override
            public boolean isChildSelectable(int i, int i1) {
                return true;
            }
    
            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                /* used to make the notifyDataSetChanged() method work */
                super.registerDataSetObserver(observer);
            }
    
    
            /* (non-Javadoc)
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             * @since Mar 20, 2013
             * @author rajeshcp 
             */
            @Override
            public void onClick(View v) {
                if(mExpandableList.isGroupExpanded((Integer)v.getTag()))
            {
                mExpandableList.collapseGroup((Integer)v.getTag());
            }else
            {
                mExpandableList.expandGroup((Integer)v.getTag());
            }
            }
        }
    
    
    }
    

    Change your MyActivity like this and let me know what else you want ?

提交回复
热议问题