如何显示一定数量的子组

时间:2014-06-28 20:22:46

标签: android expandablelistview

我有ExpandableListViewchild groups的计数因动态创建而有所不同。我需要前3 child groups默认情况下始终展开。当用户点击main group时,child groups的其余部分将会展开(我附上了一张图,说明了我需要ExpandableListView默认状态实现)。我还没有找到ExpandableListView的任何方法来实现这一目标。

有什么建议吗?

enter image description here

3 个答案:

答案 0 :(得分:2)

将您的特殊子项目复制为顶级项目,样式设置为子项目。然后,当一个顶级项目展开为真实时,将假子项目的高度设置为零。

答案 1 :(得分:2)

我和@ Pr38y有类似的想法,但没有时间对它进行编码(直到现在)。对他/她仍然感到荣幸。此ExpandableList 可防止组完全崩溃。这是通过仅根据布尔值(isGroupExpanded)切换每个组的childCount来完成的。

演示myClass:

public class myClass {
    int id;
    String name;
    public myClass(int id, String str) {
        this.id = id;
        this.name = str;
    }
}

活动(对于庞大的HashMap初始化感到抱歉):

package com.example.explvhack;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Simon on 2014 Jul 8.
 */

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandableListView expList = (ExpandableListView) findViewById(R.id.expList);

        // Create the adapter with 3 groups
        final List<String> groups = new ArrayList<String>() {{add("Group1"); add("Group2"); add("Group3");}};
        final ExpLvAdapter expAdapter = new ExpLvAdapter(getBaseContext(), groups);

        // If group is not "expanded" it displays 3 children, else displays all
        // This listener, forces it to toggle between fake expanded and collapse states
        expList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView dad, View v, int grpPosition, long id) {
                expAdapter.isGroupExpanded[grpPosition] = !expAdapter.isGroupExpanded[grpPosition];
                expAdapter.notifyDataSetChanged();

                return true;
            }
        });

        expList.setAdapter(expAdapter);

        // Populate the groups
        HashMap<String, List<myClass>> newData =
                new HashMap<String, List<myClass>>() {
                    {
                        put(groups.get(0), new ArrayList<myClass>() {
                            {
                                add(new myClass(0, "Grp1Child1"));
                                add(new myClass(1, "Grp1Child2"));
                                add(new myClass(2, "Grp1Child3"));
                                add(new myClass(3, "Grp1Child4"));
                                add(new myClass(4, "Grp1Child5"));
                                add(new myClass(5, "Grp1Child6"));
                                add(new myClass(6, "Grp1Child7"));
                                add(new myClass(7, "Grp1Child8"));
                            }
                        });
                        put(groups.get(1), new ArrayList<myClass>() {
                            {
                                add(new myClass(0, "Grp2Child1"));
                                add(new myClass(1, "Grp2Child2"));
                                add(new myClass(2, "Grp2Child3"));
                                add(new myClass(3, "Grp2Child4"));
                            }
                        });
                        put(groups.get(2), new ArrayList<myClass>() {
                            {
                                add(new myClass(0, "Grp3Child1"));
                                add(new myClass(1, "Grp3Child2"));
                                add(new myClass(2, "Grp3Child3"));
                            }
                        });

                    }
                };
        expAdapter.putAll(newData);
        expAdapter.notifyDataSetChanged();

        // Keep the groups opened
        expList.expandGroup(0);
        expList.expandGroup(1);
        expList.expandGroup(2);
    }

}

自定义适配器:

package com.example.explvhack;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Simon on 2014 Jul 8.
 */

public class ExpLvAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groups;
    private HashMap<String, List<myClass>> children = new HashMap<String, List<myClass>>();
    public boolean[] isGroupExpanded = new boolean[] {false, false, false};

    public ExpLvAdapter(Context context, final List<String> groups) {
        this.context = context;
        this.groups = groups;
    }

    public void putAll(HashMap<String, List<myClass>> newData) {
        children.putAll(newData);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = new TextView(context);

        // (Re)Use the convertView
        TextView textView = (TextView) convertView;
        textView.setPadding(60, 0, 30, 0);
        textView.setText(children.get(groups.get(groupPosition)).get(childPosition).name);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int childrenCount = (children.size() == 0) ? 0 : children.get(groups.get(groupPosition)).size();

        if (isGroupExpanded[groupPosition])
            return childrenCount;
        else
            return Math.min(3, childrenCount);
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public int getGroupCount() {
        return 3;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = new TextView(context);

        // (Re)Use the convertView
        TextView textView = (TextView) convertView;
        textView.setText(groups.get(groupPosition));

        return textView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition){
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }
}

顺便说一句,我也删除了组指示符,这样如果它是打开的话就没有混淆。我在android:groupIndicator="@null"

中使用了<ExpandableListView .../>

答案 2 :(得分:1)

将正常的线性布局与listview一起使用可能是一种更好的方法。在线性布局中单击标题时,您可以完全打包列表视图适配器的列表并调用notifyDataSetChanged,然后在下一次点击时,您可以将列表缩小为3个元素。

首次使用ExpandableListView,您可以从getChildCount()返回3,然后再输入整个列表的大小。您可以通过onGroupExpandListeneradapter本身来实现此效果。