3

インターネットで答えの解決策を見つけようとしていますが、良い解決策が見つかりません。このトピックを見ました: 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 のヘッダー) で同じことを行う方法がわかりません。

前もって感謝します。

4

3 に答える 3

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年後、それがあなたのために働くことを願っています:)

于 2014-07-07T16:15:09.580 に答える
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

    }

別のレイアウトまたは好きなものを使用できる場所。

于 2013-06-11T18:22:17.157 に答える
1

Guillermo Orellana Ruiz の上記の解決策は正常に動作しますが、「追加の子」 が子要素の配列の 0 番目の位置に追加された場合にのみ、正確なすべての子要素のみが表示され、それ以外の場合は 1 つの子要素が表示されることに注意してください。欠落、子配列または子要素の 0 番目の位置に1 つの余分な子を追加していない場合 、、

私のタスクからの私の理解から何が起こるかというと、「余分な子」が追加されると、0番目の位置がその「余分な子」を取り、その「余分な子」の代わりにその子が表示されず、ヘッダービューが表示されるということです。によって行われます:

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

        return convertView;
    } 

また、子配列に追加する余分な子は 0 番目の位置にとどまる必要があることに注意してください 。そうしないと、余分な子が子リスト ビューに表示され、0 番目の位置にある実際のデータは表示されません。

タスクにこのコードを使用したときに、子要素の1つが表示されず、欠落していて、しばらく疑問に思ったので、これを理解するのに時間がかかったので、これを投稿しましたa 承認された回答を使用する人が時間を無駄にしないように注意してください。

于 2013-10-01T07:36:06.557 に答える