从ExpandableListView(Android)在ChildView中添加标头

时间:2013-06-09 17:07:42

标签: android listview header expandablelistview children

我正在尝试通过互联网找到我的答案的解决方案,但我找不到一个好的解决方案。我看了这个主题:Android ExpandableListView Child Headers但它对我不起作用。

我现在拥有的(屏幕):

我的代码是:

private void fillData() {
    ExpandableListView lv;
    lv = (ExpandableListView) getActivity().findViewById(R.id.grades_view);
    lv.setItemsCanFocus(false);
    lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);

    View view = View.inflate(getActivity(), R.layout.list_header, null);
    lv.addHeaderView(view, null, false);

    dbh = new DatabaseHelper(getActivity());
    MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(
            getActivity(), dbh);
    mAdapter.synchronize();
    lv.setAdapter(mAdapter);
    registerForContextMenu(lv);
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private List<Subject> mSubjects = new ArrayList<Subject>();
    private List<List<Course>> mCourse = new ArrayList<List<Course>>();
    private DatabaseHelper dbh;
    private LayoutInflater inflater;

    public MyExpandableListAdapter(Context context, DatabaseHelper dbh) {
        this.dbh = dbh;
        this.inflater = LayoutInflater.from(context);
    }

    public void synchronize() {

        mSubjects = dbh.getSubjects();

        for (Subject s : mSubjects) {
            mCourse.add(dbh.getCourses(s));
        }
    }

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

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

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        Course c = (Course) getChild(groupPosition, childPosition);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_item_child,
                    parent, false);

        TextView txt = (TextView) convertView.findViewById(R.id.code);
        txt.setText(c.getCode());
        TextView txt1 = (TextView) convertView.findViewById(R.id.ects);
        txt1.setText(String.valueOf(c.getEcts()));
        TextView txt2 = (TextView) convertView.findViewById(R.id.grade);

        if (c.getGrade() == -1) {
            txt2.setText("-");
        } else {
            txt2.setText(String.valueOf(c.getGrade()));
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mCourse.get(groupPosition).size();
    }

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

    @Override
    public int getGroupCount() {
        return mSubjects.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return mSubjects.get(groupPosition).hashCode();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        Subject s = (Subject) getGroup(groupPosition);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_item, parent,
                    false);

        TextView txt1 = (TextView) convertView.findViewById(R.id.name);
        txt1.setText(String.valueOf(s.getName()));
        TextView txt2 = (TextView) convertView.findViewById(R.id.semester);
        txt2.setText(String.valueOf(s.getSemester()));

        return convertView;

    }

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

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

}

我们可以看到,要使用我使用的第一级标题(Name和Sem):

View view = View.inflate(getActivity(), R.layout.list_header, null);
lv.addHeaderView(view, null, false);

但我不知道如何对孩子们这样做(带有Code,ECTS和Grade的标题)。

提前谢谢。

3 个答案:

答案 0 :(得分:5)

1.-在您的xml组中包含标题,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    //Your elementsof group
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/headerList"
    android:orientation="horizontal" >
    //the elements of your header (ID,counter, description, etc..)
</LinearLayout>

2.-现在,在方法getGroupView()...

中使用该ID
LinearLayout headerList = (LinearLayout) view.findViewById(R.id.headerList);
    if(isExpanded)
        headerList.setVisibility(View.VISIBLE);
    else
        headerList.setVisibility(View.GONE);

一年后,我希望它适合你:)

答案 1 :(得分:1)

只需添加“额外孩子”

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (childPosition == 0)
            return null;

        return mCourse.get(groupPosition).get(childPosition - 1);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        if (childPosition == 0)
            return 0;

        return getChild(groupPosition, childPosition).hashCode();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (childPosition == 0) {
            convertView = inflater.inflate(R.layout.list_item_child, null);

            // your special header

            return convertView;
        } 

        // rest of your method

    }

您可以拥有不同的布局或任何您喜欢的地方。

答案 2 :(得分:1)

Guillermo Orellana Ruiz的上述解决方案可以正常工作,但请注意,只有&#34; &#34;额外的孩子&#34; 被添加到第0个位置的子元素数组中,然后只显示所有确切的子元素,否则将丢失一个子元素,如果你是不要将一个额外的孩子添加到子数组或子元素的第0个位置,

从我的任务中理解的是,当一个额外的孩子&#34;添加第0个位置需要&#34;额外的孩子&#34;那个孩子将不会被展示,而不是那个&#34;额外的孩子&#34;显示标题视图,这是通过以下方式完成的:

      if (childPosition == 0) {
       // your header
        convertView = inflater.inflate(R.layout.list_item_child, null);            

        return convertView;
    } 

另请注意,添加到子数组的额外子项必须保持在第0位,否则额外的子项将显示在子列表视图中,而您在第0位的实际数据将不会显示

我刚刚提到这个,因为当我使用这个代码完成我的任务时,其中一个子元素没有显示,它丢失了,我想了一会儿,所以花了一些时间让我理解这一点,所以我只是将此作为注释发布,以便使用已接受答案的人不得浪费他/她的时间。