ExpandableListView使用体验

S70608-160016.jpg

1. 在布局文件中配置

android:groupIndicator="@null"
null代表不要组的标志

 <ExpandableListView
        android:groupIndicator="@null"
        android:id="@+id/lv_question"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

2. 适配器之 BaseExpandableListAdapter

public class QueationAnswerAdapter extends BaseExpandableListAdapter {

//两个数组中存放展示的内容
    private String[] question = {};
    private String[] answer = { };


    //获取组的个数 相当于getcount
    @Override
    public int getGroupCount() {
        return question.length;
    }

    /*获取组的孩子的个数*/
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    /*获取组*/
    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    /*获取孩子*/
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    /*获取组的id*/
    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    /*获取孩子的id*/
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    /*标识是否设置了id TRUE设置  FALSE 没有设置*/
    @Override
    public boolean hasStableIds() {
        return false;
    }

    /*设置组的样式*/
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(parent.getContext(), R.layout.item_textview, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_item_textview);
        textView.setText(question[groupPosition]);
        return view;
    }

    /*设置孩子的样式*/
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(parent.getContext(), R.layout.item_textview_answer, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_item_textview_answer);
        textView.setText(answer[groupPosition]);
        return view;
    }

    /*设置孩子是否可以点击*/
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

3. 监听事件

 lvQuestion.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (groupPosition != customPosition) {
                    lvQuestion.collapseGroup(customPosition);
                }

                if (lvQuestion.isGroupExpanded(groupPosition)) {
                    lvQuestion.collapseGroup(groupPosition);
                    customPosition = -1;
                } else {
                    lvQuestion.expandGroup(groupPosition);
                    customPosition = groupPosition;
                }
                return true;
            }
        });

4. 默认展开全部群组

遍历群组 然后全部展开

for(int i = 0; i < expandableListAdapter.getGroupCount(); i++){  
                          
   expandableListView.expandGroup(i);  
                          
}

推荐阅读更多精彩内容