如何从Activity / Fragment

时间:2017-03-01 07:44:25

标签: android interface android-recyclerview android-viewholder

我有三个班级。片段,RecyclerViewAdapter和RecyclerViewHolder。他们以通常的方式工作。但是现在我遇到了一个问题,我需要在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);

1 个答案:

答案 0 :(得分:2)

您可以调用此方法(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
}

我希望它对你有所帮助。