【问题标题】:How to call ViewHolder method from Activity/Fragment如何从 Activity/Fragment 调用 ViewHolder 方法
【发布时间】:2017-07-20 10:30:25
【问题描述】:

我有三个课程,即。一个片段、一个 RecyclerViewAdapter 和一个 RecyclerViewHolder。他们以通常的方式工作。但是现在我遇到了一个问题,我需要从上述 Activity 中调用 ViewHolder 中的一个方法。

我正在考虑使用回调,但我尝试了,但它不起作用。这是我到目前为止所做的:

界面

public interface TreeBottomListener {
    enum Status {FAILED, SUCCESS, PRE_REQUEST}
    void onResponse(Status status);
}

ViewHolder

public class TreeBottomViewHolder extends RecyclerView.ViewHolder implements TreeBottomListener{
    @BindView(R.id.tree_bottom_progress) ProgressBar bottomProgress;
    @BindView(R.id.tree_error_button) Button moreRetryButton;
    private TreeAdapterListener listener;
    private final String TAG = "TreeBottomViewHolder";

    public TreeBottomViewHolder(View itemView, final TreeAdapterListener listener) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.listener = listener;
        setUpViews();
    }

    private void setUpViews() {
        bottomProgress.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor
                (itemView.getContext(), R.color.colorAccent), PorterDuff.Mode.SRC_IN);

        moreRetryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setVisibility(View.GONE);
                listener.onLoadMoreClicked();
            }
        });
    }

    @Override
    public void onResponse(Status status) {
        processViews(status);
    }

    private void processViews(Status status) {
    //  This method is never called
        Log.i(TAG, "onResponse: with status " + status);
        if (status == Status.FAILED) {
            bottomProgress.setVisibility(View.GONE);
            moreRetryButton.setVisibility(View.VISIBLE);

        } else if (status == Status.SUCCESS) {
            bottomProgress.setVisibility(View.GONE);

        } else if (status == Status.PRE_REQUEST) {
            bottomProgress.setVisibility(View.VISIBLE);
        }
    }
}

片段

private TreeBottomListener bottomListener = bottomCallbacks;

private static TreeBottomListener bottomCallbacks = new TreeBottomListener() {
        @Override
        public void onResponse(Status status) {

        }
    };

// I do this say when a network request is about to be sent
bottomListener.onResponse(TreeBottomListener.Status.PRE_REQUEST);

【问题讨论】:

  • 参考此link 供您参考
  • @vijaychhalotre,谢谢。我以前见过,但我想做的是相反的,即从 Fragment/Activity 调用 ViewHolder 方法。
  • 你不应该这样做。这将使您的代码紧密耦合并且非常复杂。适配器任务应该是显示列表或recyclerView中的项目。 Activity 不应直接与 Adapter 或 ViewHolder 交互。如果您想刷新列表中的项目,请更改适配器数据列表并调用 notifydatasetChange 方法。

标签: android interface android-recyclerview android-viewholder


【解决方案1】:

你可以调用这个方法(YourAdapter.TreeBottomViewHolder)recyclerView.findViewHolderForLayoutPosition(position); 它返回 ViewHolder,您可以使用以下代码检查项目位置:

    private boolean isCurrentListViewItemVisible(int position) {
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    return first <= position && position <= last;
}

那么您可以执行以下操作:

TreeBottomViewHolder holder = (YourAdapter.TreeBottomViewHolder)recyclerView.findViewHolderForLayoutPosition(position);

if(isCurrentListViewItemVisible(position)){
    // you can access your views here
}

希望对你有帮助。

【讨论】:

  • 由于某种未知原因,它返回 null。
猜你喜欢
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多