首页 > 解决方案 > 如何为每个 recyclerview 项目的按钮添加功能?

问题描述

我正在编写一个android代码,其中单击recyclerview上的按钮时,它应该将其定向到其他一些活动。该程序应将控件重定向到每个 recyclerview 项目的不同活动。我已经成功地将按钮添加到活动的模板中,但是,我无法理解如何为每个按钮添加功能。我随函附上我在项目中包含的不同文件。如果有人可以指导我如何从这里开始,那将非常有帮助。

ProductPage1.java

package com.agnik.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class ProductPage1 extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
        exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
        exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
        exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
        exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
        exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
        exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
        exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
        exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));
        exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));
        exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));
        exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

    }
}

ExampleAdapter.java

package com.agnik.example.myapplication4;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

    private ArrayList<ExampleItem> mExampleList;


    public static class ExampleViewHolder extends RecyclerView.ViewHolder
    {
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;
        public Button mButton;

        public ExampleViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
            mButton = itemView.findViewById(R.id.mybutton);


        }
    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList)
    {

        mExampleList = exampleList;

    }

    @NonNull
    @Override
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;

    }

    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {

        ExampleItem currentItem = mExampleList.get(position);
        holder.mImageView.setImageResource(currentItem.getImageResource());
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());

    }

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

    }







}

ExampleItem.java

    package com.agnik.example.myapplication4;

    public class ExampleItem {

        private int mImageResource;
        private String mText1;
        private String mText2;

        public ExampleItem(int imageResource, String text1, String text2) {
            mImageResource = imageResource;
            mText1 = text1;
            mText2 = text2;
        }

        public int getImageResource() {
            return mImageResource;
        }

        public String getText1() {
            return mText1;
        }

        public String getText2() {
            return mText2;
        }

    }

activity_product_page1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductPage1">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#008080"
        android:padding="4dp"
        android:scrollbars="vertical" />

</RelativeLayout>

example_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/imageView"
            android:text="Line 1"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_toRightOf="@+id/imageView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/imageView"
            android:text="Line 2"
            android:textSize="15sp"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@+id/imageView" />

        <Button
            android:id="@+id/mybutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/imageView"
            android:text="Purchase"
            android:textSize="15sp"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@+id/imageView"  />


    </RelativeLayout>

</androidx.cardview.widget.CardView>

activity_product_page1.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductPage1">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#008080"
        android:padding="4dp"
        android:scrollbars="vertical" />

</RelativeLayout>

在此处输入图像描述

编辑:

正如 Phil 所提到的,我需要将 setOnClickListener 添加到 holder 对象中。但是,当我在 ExampleAdapter.java 上编写代码时,我无法理解如何将控制权从 ProductPage1.class 转移到 SomeOtherActivity.class?

   holder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (position) {
                    case 1:                 

                        Intent i = new Intent(ProductPage1.class, PageDemo1_1.class);
                        startActivity(i);
                        break;
                    case 2:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_2.class);
                        startActivity(i);
                        break;
                    case 3:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_3.class);
                        startActivity(i);
                        break;
                    case 4:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_4.class);
                        startActivity(i);
                        break;
                    case 5:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_5.class);
                        startActivity(i);
                        break;
                    case 6:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_6.class);
                        startActivity(i);
                        break;
                    case 7:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_7.class);
                        startActivity(i);
                        break;
                    default:
                        Intent i = new Intent(ProductPage1.class, PageDemo1_3.class);
                        startActivity(i);
                        break;
                }



            }
        });

标签: javaandroidandroid-studioandroid-recyclerviewandroid-button

解决方案


您必须在 ExampleAdapter 的 onBindViewHolder 方法中执行此操作。

作为一个例子,你可以这样做:

@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {

    ExampleItem currentItem = mExampleList.get(position);
    holder.mImageView.setImageResource(currentItem.getImageResource());
    holder.mTextView1.setText(currentItem.getText1());
    holder.mTextView2.setText(currentItem.getText2());
    holder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // your code here
            }
    });

}

在 onBindViewHolder() 方法内部“发生了逻辑”。在那里,您可以为每个 RecyclerView 项目设置所有内容。


推荐阅读