Is it possible to programatically click RecyclerView?

Asked 2 years ago, Updated 2 years ago, 99 views

RecyclerView is being applied. The current congestion is that we want an onClick event that forces us to click on the next item after a particular event.

As I googled, there was a way to stackoverflow as follows.

recyclerView.findViewHolderForAdapterPosition.itemView.performClick();

But I applied it to Activity, but it doesn't apply to me, so I don't know how to use it.

ViewHolder

public classViewholder extensionsRecyclerView.Viewholder implementationsView.OnClickListener {
    private TextView virtNo;
    private TextView score01;
    private TextView score02;
    private TextView score03;
    private TextView totalScore;
    private LinearLayout linearLayout;

    public ViewHolder(View itemView) {
        super(itemView);
        this.virtNo = (TextView) itemView.findViewById(R.id.tv_virtNo);
        this.score01 = (TextView) itemView.findViewById(R.id.tv_score01);
        this.score02 = (TextView) itemView.findViewById(R.id.tv_score02);
        this.score03 = (TextView) itemView.findViewById(R.id.tv_score03);
        this.totalScore = (TextView) itemView.findViewById(R.id.tv_totalScore);
        this.linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_score);

        virtNo.setOnClickListener(this);
        score01.setOnClickListener(this);
        score02.setOnClickListener(this);
        score03.setOnClickListener(this);
        score10.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onClickListener.onClick(v, getAdapterPosition(), items.get(getAdapterPosition()));
    }
}

onClickListener

public void setOnClickListener<Score>onClickListener) {
    this.onClickListener = onClickListener;
}

public interface OnClickListener<T> {
    void onClick(View v, int position, T item);
}

onBindViewHolder

 @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Score item = items.get(position);
    holder.virtNo.setText(item.virtNo);
    holder.score01.setText(item.score01);
    holder.score02.setText(item.score02);
    holder.score03.setText(item.score03);
    holder.totalScore.setText(itemSum(item));
    }
}

android recyclerview

2022-09-21 20:21

1 Answers

Since you said NullPointerException wasn't released, recyclerView.findViewHolderForAdapterPosition(position) You have successfully imported the view holder. :)

recyclerView.findViewHolderForAdapterPosition(position) If you call like this, the ViewHolder is returned as a whole. If you call performanceClick, it seems that the entire layout was covered by the click, so it didn't work as you wanted.

I tested it with ViewHolder, which has only TextView It worked well.

final CustomAdapter.ViewHolder holder = (CustomAdapter.ViewHolder) mRecyclerView.findViewHolderForPosition(0);
holder.getTextView().performClick();
public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        public ViewHolder(View v) {
            super(v);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Element " + getPosition() + " clicked.");
                }
            });
            textView = (TextView) v.findViewById(R.id.textView);
        }

        public TextView getTextView() {
            return textView;
        }
    }

View xml is shown below. I wrapped the TextView with FrameLayout, but the performanceClick didn't work. After removing FrameLayout and leaving only TextView, performanceClick worked.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/list_item_height"
    android:layout_marginLeft="@dimen/margin_medium"
    android:layout_marginRight="@dimen/margin_medium"
    android:gravity="center_vertical"
    android:text="@string/element_text" />

I hope it helps you even a little bit.


2022-09-21 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.