我正在搜索以编程方式单击方法的函数。所以我找到了一些方法。 “ performClick()”

像那样 :

recyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();


但这对我来说不起作用。我找不到解决方法。如何在活动中使用performClick?

我的适配器-ViewHolder:

public class ViewHolder extends RecyclerView.ViewHolder implements View.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()));
    }
}

public void setOnClickListener(OnClickListener<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));
    }
}

最佳答案

我试图使事情在这里易于理解,这是一个完整的示例,用于监听RecyclerView中单个项目的click事件,还有其他方法也可以做到这一点。此代码有效,如果有任何疑问,可以在注释中对其进行修改。代码也可以从GitHub获得

public class RecyclerViewOneActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_one);
        initializeUI();
    }

    private void initializeUI() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.RecyclerViewOneActivity_RecyclerView);
        RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("first");
        strings.add("second");
        MyAdapter adapter = new MyAdapter(getApplicationContext(), strings);
        recyclerView.setAdapter(adapter);

    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private Context context;
        private ArrayList<String> strings;
        private LayoutInflater layoutInflater;

        public MyAdapter(Context context, ArrayList<String> strings) {
            this.context = context;
            this.strings = strings;
            layoutInflater = LayoutInflater.from(this.context);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = layoutInflater.inflate(R.layout.single_item_recycler_view_one, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final String text = this.strings.get(position);
            holder.textView.setText("" + text);
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, ""+text, Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return strings.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private LinearLayout linearLayout;
            private TextView textView;

            public MyViewHolder(View itemView) {
                super(itemView);
                linearLayout = (LinearLayout) itemView.findViewById(R.id.single_item_recycler_view_one_linear_layout);
                textView = (TextView) itemView.findViewById(R.id.single_item_recycler_view_one_textView);
            }
        }
    }
}


single_item_recycler_view_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:id="@+id/single_item_recycler_view_one_linear_layout"
    android:orientation="vertical">

    <TextView
        android:id="@+id/single_item_recycler_view_one_textView"
        android:layout_width="match_parent"
        android:textColor="#000"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

09-30 11:05