如何为每个recyclerview项目的按钮添加功能?

时间:2019-09-13 07:22:25

标签: java android android-studio android-recyclerview android-button

我正在编写一个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>

enter image description here

编辑:

正如Phil所提到的,我需要添加具有holder对象的setOnClickListener。 但是,当我在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;
                }



            }
        });

4 个答案:

答案 0 :(得分:0)

您必须在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项目设置所有内容。

答案 1 :(得分:0)

除了需要将位置作为参数之外,您还需要创建一个与普通点击侦听器相似的界面。

public interface RecyclerViewClickListener {
    void onClick(View view, int position);
}

然后您就可以用适配器来声明

private final RecyclerViewClickListener listener;

您可以在适配器中创建一个setter

public void setListner(RecyclerViewClickListener listener){
this.listener=listener;
}

答案 2 :(得分:0)

您将必须在适配器中添加一个回调接口,并将该接口的实例从活动传递到适配器构造函数。当您单击recyclerview项的不同ViewGroup时,请调用接口的方法,并且Activity中应有一个实现。您的代码将如下所示:

活动

SwingUtilities.invokeLater

适配器

    public class ProductPage1 extends AppCompatActivity implements ClickCallback {

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

        }

       @Override
       onItemClick(int position, View view){
        switch(view.getId()){
           case R.id.mImageView:
         //do your view click events here
            break;
           case R.id.mImageView2:
         //do your view click events here
           break;
         //so on
       }
 }

希望您得到了答案。快乐编码:)

答案 3 :(得分:0)

此代码在每个单击的项目上显示一个对话框

@NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_item, null);
        final ItemViewHolder viewHolder = new ItemViewHolder(view);
        dialoge = new Dialog(parent.getContext());
        dialoge.setContentView(R.layout.dialog);
        dialoge.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        viewHolder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setDialog(dialoge, viewHolder);
                dialoge.show();
            }

        });

        return viewHolder;
    }

,在方法setDialog()中,您将使用

获取当前项目位置
final RecyclerItem currentItem = itemList.get(viewHolder.getAdapterPosition());

但是我认为最好的做法是考虑使用一个表面,这是一个很好的教程,可以遵循tutorialRecyclerItemOnClickListenerInterface

希望对您有帮助

  

编辑:从onClickListnere启动活动:

Intent myIntent = new Intent(parent.getContext(),yourActivityName.class);
                myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myIntent);