Android - 使用ExpandableListAdapter正确放置侦听器

时间:2016-07-31 15:41:26

标签: android android-layout listview android-adapter

我有一个名为 ExpandableListAdapter 的类,它扩展了BaseExpandableListAdapter。我的getChildView方法如下所示:

    @Override
   public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    tv_questionAmountResult = (TextView) convertView.findViewById(R.id.tv_questionAmountResult);
    tv_correctAmountResult = (TextView) convertView.findViewById(R.id.tv_correctAmountResult);
    tv_wrongAmountResult = (TextView) convertView.findViewById(R.id.tv_wrongAmountResult);

    bt_allQuestions = (Button) convertView.findViewById(R.id.bt_allQuestions);
    bt_correctQuestions = (Button) convertView.findViewById(R.id.bt_correctQuestions);
    bt_wrongQuestions = (Button) convertView.findViewById(R.id.bt_wrongQuestions);

    return convertView;
}

我的其他班级 ActTraining 正在使用onCreate方法中的ExpandableListAdapter:

 ExpandableListAdapter listAdapter;
 ExpandableListView expListView;

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.topicExpandableListView);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

现在我想为3个按钮添加OnClickListener,但我不确定正确的位置。 我是否必须在我的getChildView或ActTraining类中添加监听器?

1 个答案:

答案 0 :(得分:0)

您必须在OnClickListener中为所有3个按钮添加ExpandableListAdapter.getChildView,因为每个childItem中都有按钮。

句柄OnClickListener的一些有用链接:

RecyclerView click listener

Set a click listener to a RecyclerView