【问题标题】:android can not get child view inside recyclerViewandroid无法在recyclerView中获取子视图
【发布时间】:2019-08-29 13:58:54
【问题描述】:

我尝试按位置获取 childView。我写了类似这样的代码

private void showDirectDebitShadow(int pos, String message) {
    boolean allowScale = false;
    if (pos > 0 && isRecyclerScrollable()) {
        templateList.getRecyclerView().post(() -> layoutManager.scrollToPosition(pos));
    }
    if (pos < layoutManager.findLastVisibleItemPosition()) {
        allowScale = false;
    } else {
        allowScale = true;
    }
    new Handler().postDelayed(() -> {
        templateList.getRecyclerView().post(() -> {
            View childView = layoutManager.findViewByPosition(pos);
            if (childView != null) {
                childView.postDelayed(() -> {
                    SwipeMenuLayout swipeMenuLayout = childView.findViewById(R.id.SwipeMenuLayout);
                    View directDebitIcon = childView.findViewById(R.id.direct_debit_icon);
                    if (swipeMenuLayout != null && directDebitIcon != null) {
                        swipeMenuLayout.smoothExpand();
                        new Handler().postDelayed(() -> showCaseView(message, directDebitIcon, getActivity(), swipeMenuLayout), 400);

                    }
                },500);

            }
        });
    }, isRecyclerScrollable() ? 500 : 100);
}

我调试了它,我的子视图不为空,但我可以从 childView 获得另一个视图(SwipeMenuLayout 和 directDebitIcon) 谁能告诉我这段代码有什么问题?

【问题讨论】:

    标签: android android-recyclerview android-linearlayout


    【解决方案1】:

    使用reyclerView.findViewHolderForAdapterPosition(position)可以访问ViewHolder,然后你就可以通过它轻松获取任何子视图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多