1

我正在尝试创建一个带有可选元素(子项)的 ExpandableListView,以便在关闭组或滚动所选元素后选择不会消失。我还没有找到任何本机解决方案,因此我尝试将元素的选定状态保存在单独的列表中,并更改 onChildClickListner 中元素的状态和颜色。问题是 - 我无法让 Listner 工作。就是进不去。但是我可以分别为每个孩子设置一个Listner。这确实有效,但不能解决我的问题,因为在这种情况下,我没有得到任何关于孩子在小组中的位置的信息。这可能只是一个小错误..这是代码:

ps 一些变量名是德语的,对此感到抱歉=) pps 我将不胜感激任何提示!

package com.example.android.apis;

import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

public class ApiDemos extends ExpandableListActivity {

    // This one works just fine if i set it for each child
    private View.OnClickListener cl1 = new View.OnClickListener() {
        public void onClick(View v) {
            if (((TextView) v).isSelected() == false) {

                ((TextView) v).setSelected(true);
                ((TextView) v).setTextColor(RED);
            } else {
                ((TextView) v).setSelected(false);
                ((TextView) v).setTextColor(WHITE);
            }
        }
    };

    // each child has a relation with one of these:
    class veranstaltungView {
        public String text;
        public boolean marked;

        public veranstaltungView(String txt) {
            text = txt;
            marked = false;
        }
    }

    // This one is set on the whole ExpandableListView. It just doesnt get
    // inside!
    private OnChildClickListener cl = new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            if (((TextView) v).isSelected() == false) {

                veranstaltungenList.get(groupPosition).get(childPosition).marked = true;
                ((TextView) v).setSelected(true);
                ((TextView) v).setTextColor(RED);
            } else {
                veranstaltungenList.get(groupPosition).get(childPosition).marked = false;
                ((TextView) v).setSelected(false);
                ((TextView) v).setTextColor(WHITE);
            }
            return false;
        }
    };

    private static final int WHITE = 0xffffffff;
    private static final int RED = 0xffbc0000;

    ExpandableListAdapter mAdapter;
    private ArrayList<ArrayList<veranstaltungView>> veranstaltungenList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // generate some elements
        veranstaltungenList = new ArrayList<ArrayList<veranstaltungView>>();
        for (int i = 0; i < 6; i++) {
            ArrayList<veranstaltungView> semester = new ArrayList<veranstaltungView>();
            for (int j = 0; j < 3; j++) {
                semester.add(new veranstaltungView("Veranstaltung " + i + "."
                        + j));
            }
            veranstaltungenList.add(semester);
        }

        // Create and set the adapter
        ExpandableListView elv = getExpandableListView();
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);

        elv.setItemsCanFocus(false);
        elv.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);

        elv.setOnChildClickListener(cl);
    }

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        // The groups
        private String[] semesterArray = { "SEMESTER1", "SEMESTER2",
                "SEMESTER3", "SEMESTER4", "SEMESTER5", "SEMESTER6" };

        public Object getChild(int groupPosition, int childPosition) {
            return veranstaltungenList.get(groupPosition).get(childPosition).text;
        }

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

        public int getChildrenCount(int groupPosition) {
            return veranstaltungenList.get(groupPosition).size();
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView textView = new TextView(ApiDemos.this);

            textView.setTextColor(WHITE);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

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

            TextView textView = null;
            textView = getGenericView();
            textView.setText((String) getChild(groupPosition, childPosition));
            textView.setClickable(true);
            textView.setSelected(veranstaltungenList.get(groupPosition).get(
                    childPosition).marked);

            if (veranstaltungenList.get(groupPosition).get(childPosition).marked == true)
                textView.setTextColor(RED);
            else
                textView.setTextColor(WHITE);

            // textView.setOnClickListener(cl1);

            return textView;
        }

        public Object getGroup(int groupPosition) {
            return semesterArray[groupPosition];
        }

        public int getGroupCount() {
            return veranstaltungenList.size();
        }

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

        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

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

        public boolean hasStableIds() {
            return true;
        }
    }
}
4

1 回答 1

2

问题是getChildView方法中的“ textView.setClickable (true); ”阻止了 OnClickListener的执行。此外,“ if (((TextView) v).isSelected() == false) ”行应更改为“ veranstaltungenList.get(groupPosition).get(childPosition).marked ”。在这些变化之后就像魅力一样!它可能不是最好的解决方案,但它确实有效。希望这对某人有帮助!

于 2010-11-29T17:32:17.943 回答