如何向每个 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;


    @Overrid

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


    }

}


月关宝盒
浏览 159回答 4
4回答

慕少森

您必须在 ExampleAdapter 的 onBindViewHolder 方法内执行此操作。作为一个例子,你可以这样做:@Overridepublic void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {&nbsp; &nbsp; ExampleItem currentItem = mExampleList.get(position);&nbsp; &nbsp; holder.mImageView.setImageResource(currentItem.getImageResource());&nbsp; &nbsp; holder.mTextView1.setText(currentItem.getText1());&nbsp; &nbsp; holder.mTextView2.setText(currentItem.getText2());&nbsp; &nbsp; holder.mButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // your code here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}在 onBindViewHolder() 方法内部“逻辑发生”。在那里您可以为每个 RecyclerView 项目设置所有内容。

明月笑刀无情

此代码在单击的每个项目上显示一个对话框@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);

跃然一笑

您需要创建一个与普通点击侦听器类似的界面,但它也有位置作为参数。public&nbsp;interface&nbsp;RecyclerViewClickListener&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;void&nbsp;onClick(View&nbsp;view,&nbsp;int&nbsp;position); }然后你可以在你的适配器中声明private&nbsp;final&nbsp;RecyclerViewClickListener&nbsp;listener;您可以在适配器中创建一个设置器public&nbsp;void&nbsp;setListner(RecyclerViewClickListener&nbsp;listener){ this.listener=listener; }

慕斯王

您必须将回调接口添加到适配器中,并将该接口的实例从活动传递到适配器构造函数。当您单击 recyclerview 项的不同 ViewGroup 时调用接口的方法,并且 Activity 中应该有一个实现。你的代码看起来像这样活动&nbsp; &nbsp; public class ProductPage1 extends AppCompatActivity implements ClickCallback {&nbsp; &nbsp; &nbsp; &nbsp; private RecyclerView mRecyclerView;&nbsp; &nbsp; &nbsp; &nbsp; private RecyclerView.Adapter mAdapter;&nbsp; &nbsp; &nbsp; &nbsp; private RecyclerView.LayoutManager mLayoutManager;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_product_page1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<ExampleItem> exampleList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.mickeymouse, "Line1" , "Line2"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.donalduck, "Line3" , "Line4"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exampleList.add(new ExampleItem(R.drawable.popey, "Line5" , "Line6"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView = findViewById(R.id.recyclerView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView.setHasFixedSize(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLayoutManager = new LinearLayoutManager(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mAdapter = new ExampleAdapter(exampleList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView.setLayoutManager(mLayoutManager);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mRecyclerView.setAdapter(mAdapter);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp;onItemClick(int position, View view){&nbsp; &nbsp; &nbsp; &nbsp; switch(view.getId()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case R.id.mImageView:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//do your view click events here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case R.id.mImageView2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//do your view click events here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//so on&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;}适配器public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {&nbsp; &nbsp; private ArrayList<ExampleItem> mExampleList;&nbsp; &nbsp; public static class ExampleViewHolder extends RecyclerView.ViewHolder&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public ImageView mImageView;&nbsp; &nbsp; &nbsp; &nbsp; public TextView mTextView1;&nbsp; &nbsp; &nbsp; &nbsp; public TextView mTextView2;&nbsp; &nbsp; &nbsp; &nbsp; public Button mButton;&nbsp; &nbsp; &nbsp; &nbsp; public ExampleViewHolder(@NonNull View itemView,ClickCallback clickCallback) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);this.clickCallback=clickCallback;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mImageView = itemView.findViewById(R.id.imageView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView1 = itemView.findViewById(R.id.textView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView2 = itemView.findViewById(R.id.textView2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mButton = itemView.findViewById(R.id.mybutton);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public ExampleAdapter(ArrayList<ExampleItem> exampleList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mExampleList = exampleList;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; ExampleViewHolder evh = new ExampleViewHolder(v);&nbsp; &nbsp; &nbsp; &nbsp; return evh;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; ExampleItem currentItem = mExampleList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; holder.mImageView.setImageResource(currentItem.getImageResource());&nbsp; &nbsp; &nbsp; &nbsp; holder.mTextView1.setText(currentItem.getText1());&nbsp; &nbsp; &nbsp; &nbsp; holder.mTextView2.setText(currentItem.getText2());holder.mImageView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickCallback.onItemClick(position, holder.mImageView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //and same for other views&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return mExampleList.size();&nbsp; &nbsp; }&nbsp; &nbsp; public interface ClickCallback{&nbsp; &nbsp; &nbsp; &nbsp; void onItemClick(int position, View view);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//here you can send the object of the list at the position if you require&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//that in activity&nbsp; &nbsp; }}希望你得到答案。快乐编码:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java