2015-08-08 92 views
0

我在我的視圖中有一個ExpandableListView。我已經實現了它的小孩點擊監聽器,它的工作完美。如何以編程方式執行ExpandableListView子項目單擊

@Override 
public boolean onChildClick(ExpandableListView parent, View view, 
     int groupPosition, int childPosition, long id) { 
} 

現在,我還希望以編程方式執行子點擊。有沒有可能以編程方式調用它?

我已經通過我回convertViewgetChild()方法,並通過對執行上點擊試過,

((View)updateListAdapter.getChild(0, 2)).performClick(); 

,但沒有運氣。它不叫onChildClick()方法。

回答

1

ExpandableListView確實有一個方法

public boolean performItemClick (View v, int position, long id) 

但只有一個位置參數;小組/小孩在哪裏?

我看了一下代碼,它想要的是一個平板的位置。所以,這樣的事情應該工作(對不起,我還沒有嘗試過):

long packedPosition = ExpandableListView.getPackedPositionForChild(group, child); 
int flatPosition = expListView.getFlatListPosition(packedPosition); 
expListView.performItemClick(view, flatPosition, childId); 
+0

這是什麼意思? performItemClick(view - > this) –

+0

@AlpAltunel這是您想要點擊的項目的視圖。我離開了這一步,但顯然你需要視圖來完成這項工作。這個答案解釋得比我更好:https://stackoverflow.com/a/28335106/4504191 –

0

是其可能的,你需要從ExpandableListView然後performClick()得到ChildView,如:

expandableList.getExpandableListAdapter().getChildView(1, 0, true, null, null).performClick(); 

它運作良好...

一次ChildView是在你的口袋裏,那麼你還可以訪問&發揮其子視圖,如:

(expandableList.getExpandableListAdapter().getChildView(1, 0, true, null, null).findViewById(R.id.btn_continue)).performClick(); 

希望能幫到你

相關問題