如何将Expandable List默认状态设置为Expanded?

时间:2015-12-07 07:27:49

标签: android expandablelistadapter

我所拥有的只是3个单独的可扩展列表,在我的相同布局中扩展了BaseExpandableListAdapter,它正在按预期工作,但我希望默认情况下使所有可扩展列表处于展开状态,并希望防止它们崩溃。

public class MainActivity extends ActionBarActivity {

private ExpandableListView subtask_explist , assignto_explist , note_explist;

CheckBox checkBox_card ,checkBox_subtask ;
TextView card_textview;
LinearLayout linearLayout_card ;
RelativeLayout relativeLayout_card;
private android.support.v7.widget.Toolbar toolbar;
Context context;

private ArrayList<String> subtask_parent = new ArrayList<String>();
private ArrayList<String> subtask_child = new ArrayList<String>();

private ArrayList<String> assignto_parent = new ArrayList<String>();
private ArrayList<HashMap> assignto_child = new ArrayList<HashMap>();

private ArrayList<String> note_parent = new ArrayList<String>();
private ArrayList<HashMap> note_child = new ArrayList<HashMap>();

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

    context = getApplicationContext();

    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);


    subtask_explist = (ExpandableListView) findViewById(R.id.subtask_list_id);
    subtask_explist.setGroupIndicator(null);
    subtask_explist.setClickable(true);

    assignto_explist = (ExpandableListView) findViewById(R.id.assignto_list_id);
    assignto_explist.setGroupIndicator(null);
    assignto_explist.setClickable(true);

    note_explist = (ExpandableListView) findViewById(R.id.note_list_id);
    note_explist.setGroupIndicator(null);
    note_explist.setClickable(true);

    /*linearLayout_card = (LinearLayout) findViewById(R.id.card_layout);*/
    relativeLayout_card = (RelativeLayout) findViewById(R.id.card_layout);

    checkBox_card = (CheckBox) findViewById(R.id.cb_card);
    card_textview = (TextView) findViewById(R.id.card_text);
    checkBox_subtask = (CheckBox) findViewById(R.id.cb_subtask);

    checkBox_card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBox_card.isChecked()){
                relativeLayout_card.setBackgroundColor(getResources().getColor(R.color.selected_color));
                card_textview.setBackground(getResources().getDrawable(R.drawable.border_selected));
                card_textview.setPadding(23, 0, 0, 0);
            }
            else{
                relativeLayout_card.setBackgroundColor(getResources().getColor(R.color.white_color));
                card_textview.setBackground(getResources().getDrawable(R.drawable.border));
                card_textview.setPadding(23, 0, 0, 0);
            }
        }
    });

    setSubtaskParentData();
    setSubtaskChildData();

    setAssigntoParentData();
    setAssigntoChildData();

    setNoteParentData();
    setNoteChildData();

    setAdapters();
    setOnclickListner();

}

private void setOnclickListner() {

    subtask_explist.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);
            return false;
        }
    });

    assignto_explist.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);
            return false;
        }
    });

    note_explist.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition);
            return false;
        }
    });
}

private void setAdapters() {
    // Create the Adapter
    SubtaskExpandableAdapter subtaskExpandableAdapter = new SubtaskExpandableAdapter(subtask_parent, subtask_child);
    subtaskExpandableAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    subtask_explist.setAdapter(subtaskExpandableAdapter);
    for (int i=0; i < subtaskExpandableAdapter.getGroupCount(); i++)
        subtask_explist.expandGroup(i);

    AssigntoExpandableAdapter assigntoExpandableAdapter = new AssigntoExpandableAdapter(context , assignto_parent, assignto_child);
    assigntoExpandableAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    assignto_explist.setAdapter(assigntoExpandableAdapter);

    NoteExpandableAdapter noteExpandableAdapter = new NoteExpandableAdapter(note_parent, note_child);
    noteExpandableAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
    note_explist.setAdapter(noteExpandableAdapter);
}

private void setNoteChildData() {
    HashMap<String, String> note = new HashMap<String, String>();

    note.put("title","Rafi");
    note.put("msg", "Secondary line text Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam massa quam.");
    note_child.add(note);

    note = new HashMap<String, String>();
    note.put("title","Prem");
    note.put("msg","Secondary line text Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam massa quam.");
    note_child.add(note);
}

private void setNoteParentData() {
    note_parent.add("ADD A NOTE");
}


private void setAssigntoChildData() {
    HashMap<String, String> assignto_singleitem = new HashMap<String, String>();

    assignto_singleitem.put("title","Rafi");
    assignto_singleitem.put("image", "images");
    assignto_child.add(assignto_singleitem);

    assignto_singleitem = new HashMap<String, String>();
    assignto_singleitem.put("title","Prem");
    assignto_singleitem.put("image","images2");
    assignto_child.add(assignto_singleitem);
}

private void setAssigntoParentData() {
    assignto_parent.add("ASSIGN TO");
}

private void setSubtaskParentData() {
    subtask_parent.add("ADD SUBTASKS");
}

private void setSubtaskChildData() {
    subtask_child.add("Single line item");
    subtask_child.add("Single line item");
    subtask_child.add("Single line item");
}


private void setListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

        totalHeight += groupItem.getMeasuredHeight();

        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                totalHeight += listItem.getMeasuredHeight();

            }
        }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

}

}

1 个答案:

答案 0 :(得分:1)

ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv_main);
elv.setAdapter(adapter);
for(int i=0; i < adapter.getGroupCount(); i++)
    elv.expandGroup(i);
相关问题