2016-07-31 42 views
0

我有一个名为的类,ExpandableListAdapter它扩展了BaseExpandableListAdapter。我getChildView方法是这样的:Android - 使用ExpandableListAdapter正确放置侦听器

@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); 

现在我想补充OnClickListener的3个按键,但我米不知道正确的位置。 我是否必须在我的getChildView或ActTraining类中添加侦听器?

回答

0

对于所有3个按钮,您必须在您的ExpandableListAdapter.getChildView中放置OnClickListener,因为您在每个childItem中都有它们的按钮。

的手柄OnClickListener一些有用的链接:

RecyclerView click listener

Set a click listener to a RecyclerView

+0

的问题是,我的ExpandableListAdapter没有延伸活动。所以我不能使用例如startActivity()。 –

+0

你可以创建一个'callback'方法并将其放入一个接口并在你的'onClickListener'中调用它,然后在你的活动中实现回调。我编辑了答案并添加了一些有用的链接。 – SadeghAlavizadeh