gpt4 book ai didi

android - 如何从 Activity/Fragment 调用 ViewHolder 方法

转载 作者:行者123 更新时间:2023-12-05 00:21:46 24 4
gpt4 key购买 nike

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

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

接口(interface)

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

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

最佳答案

你可以调用这个方法(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
}

我希望它对你有帮助。

关于android - 如何从 Activity/Fragment 调用 ViewHolder 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42525925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com