自定义ExpandableListView:基于参数的父级和子级上的不同文本颜色

时间:2013-09-19 09:33:41

标签: android expandablelistview listadapter expandablelistadapter

我想创建一个自定义的ExpandableListView,如下所示。

我正在开发一个简单的问答游戏应用程序。

在游戏结束时,我想在ExpandableListView中显示问题及其答案(每个问题都是父母,单击一个问题会打开其子项,这是该问题的答案)。

问题和答案必须采用不同的颜色(将来,可能会有不同的布局和不同的图像等):RED用于错误回答的问题和错误的选择答案,GREEN用于正确回答的问题和正确选择的答案,BLACK for用户没有选择的错误答案。

为此,我创建了一个简单的自定义类。

public class MyListItem {   
// the text of the item, that can either be a question or an answer
public String text;

// 0 = not chosen and wrong (BLACK)
// 1 = correct (GREEN)
// 2 = wrong (RED)
public int textColor = 0;
}

然后,我为ExpandableListAdapater创建了一个类

public class MyExpandableListAdapter extends BaseExpandableListAdapter { ... }

我从我的应用程序类调用

    [...]
    private HashMap<MyListItem, ArrayList<MyListItem>> listOfGameAnswers;
    listAdapter = new MyExpandableListAdapter(this, listOfGameAnswers);
    [...]

但是,我无法使MyExpandableListAdapter工作(如果需要,我可以提供代码)。

我的方法是正确的还是MyExpandableListAdapter必须输入类似

的内容

地图&lt;字符串,其他内容列表&gt; ?

有没有更好的方法来实现它?

P.S。:我在这里讨论了改变文字颜色的问题 How to change text color in some headers in Expandable List

非常感谢

更新:解决方案

我接受了下面的解决方案,因为它很简单,应该可以正常使用。

无论如何,我设法修复了我的代码,如下所示:

@Override
public Object getGroup(int groupPosition) {

    ArrayList<MyListItem> myKeyList = new ArrayList<MyListItem>(listOfQuestions.keySet());

    return myKeyList.get(groupPosition);
}


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

    // get KEY
    MyListItem t = (MyListItem) getGroup(groupPosition);

    //get arraylist of my elements from key (which is an element!)
    ArrayList<MyListItem> mylist =  this.listOfQuestions.get(t);

    // return child
    return mylist.get(childPosition);

}

2 个答案:

答案 0 :(得分:2)

//base class that holds data
public class QuestionWithAnswers {  
  public String question;
  public int rightAnswer = 0;
  List<String> answers = new ArrayList(4);

  public QuestionWithAnswers(String question, int rightAnswer, List<String> answers) {
     this.question = question;
     this.rightAnswer = rightAnswer;
     this.answers.addAll(answers);     
  }

  public QuestionWithAnwsers(String question, int rightAnswer, String answers) {
     this.question = question;
     this.rightAnswer = rightAnswer;
     this.answers.addAll(Arrays.asList(answers));     
  } 
}

然后在你的适配器中:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    final Context context;
    final List<QuestionWithAnswers> data;
    public MyExpandableListAdapter(Context context, List<QuestionWithAnswers> data) {
        this.context = context;
        this.data = data;
    }

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

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

    @Override
    public QuestionWithAnswers getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).answers.get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //processing views
        return SomeView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //processing views
        int rightAnswer = data.get(groupPosition).rightAnswer;
        if (childPosition == rightAnswer){
            textViewWithAnswer.setTextColor(Color.GREEN);
        } else {
            textViewWithAnswer.setTextColor(Color.RED);
        }
        return SomeView;
    }

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

实施getGroupViewgetChildView方法

答案 1 :(得分:1)

试试这个link ...

你会得到这样的输出 enter image description here