【问题标题】:How to programatically perform ExpandableListView child item click如何以编程方式执行 ExpandableListView 子项单击
【发布时间】:2015-08-08 06:53:53
【问题描述】:

我的视图中有一个 ExpandableListView。我已经实现了它的子点击监听器,它工作得很好。

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

现在,我还想以编程方式执行子点击。是否可以通过编程方式调用它?

我已经尝试通过在getChild() 方法上返回我的convertView 并单击该方法执行,

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

但没有运气。它没有调用onChildClick() 方法。

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    ExpandableListView 确实有方法

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

    但是只有一个位置参数;小组/孩子在哪里?

    我查看了代码,它想要的是一个 flat 位置。所以像这样的东西应该可以工作(对不起,我还没有尝试过):

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

    【讨论】:

    • 这里的视图是什么? performItemClick(view -> this)
    • @AlpAltunel 这是您要单击的项目的视图。我离开了那一步,但显然你需要这个视图来完成这项工作。这个答案比我解释得更好:stackoverflow.com/a/28335106/4504191
    【解决方案2】:

    是的,有可能,您只需从ExpandableListViewperformClick() 获取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();
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2014-01-08
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2015-03-14
      • 2020-02-11
      相关资源
      最近更新 更多