recyclerview 单击转到新页面 android 页面 info.xml 并发送产品标题

recyclerview 单击转到新页面 android 页面 info.xml 并发送产品标题 我正在尝试创建一个电子商务应用程序 如何在 RecyclerView 列表中添加 onclick 打开产品信息页面(如新活动),我还想发送产品标题和产品信息页面中的 ID,


public class adepter extends RecyclerView.Adapter<adepter.viewholder> {


    private Context mCtx;

    private List<Product> productList;


    public adepter(Context mCtx, List<Product> productList) {

        this.mCtx = mCtx;

        this.productList = productList;

    }


    @NonNull

    @Override

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

        LayoutInflater inflater=LayoutInflater.from(mCtx);

        View v =inflater.inflate(R.layout.product_list, parent, false);

        return new viewholder(v);

    }


    @Override

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

        Product product = productList.get(position);

                //loading the image

                Glide.with(mCtx)

                        .load(product.getImage())

                        .into(holder.imageView);

                holder.textViewTitle.setText(product.getTitle());

                holder.textViewShortDesc.setText(product.getShortdesc());

                holder.textViewtype.setText(String.valueOf(product.getType()));

                holder.textViewPrice.setText(String.valueOf(product.getPrice()));

    }


    @Override

    public int getItemCount() {

        return productList.size();

    }



    public class viewholder extends RecyclerView.ViewHolder{

        TextView textViewTitle, textViewShortDesc, textViewtype, textViewPrice;

        ImageView imageView;

            public viewholder(@NonNull View itemView) {

                super(itemView);

            textViewTitle = itemView.findViewById(R.id.textViewTitle);

            textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);

            textViewtype = itemView.findViewById(R.id.textViewRating);

            textViewPrice = itemView.findViewById(R.id.textViewPrice);

            imageView = itemView.findViewById(R.id.imageView);

             }

     }



}

https://img4.mukewang.com/6513f3190001e19b06550366.jpg

慕运维8079593
浏览 131回答 3
3回答

莫回无

使用 Intent 传递值,如下所示:@Overridepublic void onBindViewHolder(@NonNull viewholder holder, int position) {&nbsp; &nbsp; Product product = productList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //loading the image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Glide.with(mCtx)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .load(product.getImage())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .into(holder.imageView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.textViewTitle.setText(product.getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.textViewShortDesc.setText(product.getShortdesc());&nbsp; &nbsp; holder.textViewPrice.setText(String.valueOf(product.getPrice()));&nbsp; &nbsp; final Product p=product ;&nbsp; &nbsp; holder.itemView.setOnClickListener(new OnClickListener() {&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; Intent intent=new Intent(mCtx,InfoPage.class);&nbsp; &nbsp; intent.putExtra("prod_id",p.getID());&nbsp; &nbsp; intent.putExtra("prod_title",p.getTitle());&nbsp; &nbsp; startActivity(intent);}});}产品信息页面 Ativity 看起来像:public class InfoPage extends AppCompatActivity{&nbsp; &nbsp; WebView webview;&nbsp; &nbsp; String product_id,product_tile;&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; setContentView(R.layout.product_info_xml);&nbsp; &nbsp; &nbsp; product_id=getIntent().getStringExtra("prod_id");&nbsp; &nbsp; &nbsp; product_tile=getIntent().getStringExtra("prod_title");&nbsp; &nbsp; &nbsp; getSupportActionBar().setTitle(product_tile);&nbsp; &nbsp; &nbsp; webview=findViewbyID(R.id.myWebView);&nbsp; &nbsp; &nbsp; webview.loadUrl("http://www.YourProductUrl.com");&nbsp; &nbsp; }}产品信息_xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent">&nbsp;<WebViewView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/webview"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent" /></LinearLayout>

暮色呼如

在你的 onCreateViewHolder 添加onClickListener到你的 itemView 的相关视图&nbsp;@NonNull@Overridepublic viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; LayoutInflater inflater=LayoutInflater.from(mCtx);&nbsp; &nbsp; View v =inflater.inflate(R.layout.product_list, parent, false);&nbsp; &nbsp; viewholder vh = new viewholder(v);&nbsp; &nbsp; vh.itemView.setOnClickListener(new OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Product product = productList.get(vh.getAdapterPosition());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do the page opening here&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; });&nbsp; &nbsp; return vh;}

牛魔王的故事

你可以用简单易行的方式做到这一点定义列表项的布局并在其上设置点击侦听器。使用意图将数据从回收器发送到您的活动。公共类 adepter 扩展 RecyclerView.Adapter {&nbsp; private Context mCtx;&nbsp; &nbsp; private List<Product> productList;&nbsp; &nbsp; public adepter(Context mCtx, List<Product> productList) {&nbsp; &nbsp; &nbsp; &nbsp; this.mCtx = mCtx;&nbsp; &nbsp; &nbsp; &nbsp; this.productList = productList;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int&nbsp;&nbsp; &nbsp; viewType) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater=LayoutInflater.from(mCtx);&nbsp; &nbsp; &nbsp; &nbsp; View v =inflater.inflate(R.layout.product_list, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; return new viewholder(v);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull viewholder holder, int&nbsp;&nbsp; &nbsp; position) {&nbsp; &nbsp; &nbsp; &nbsp; Product product = productList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //loading the image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Glide.with(mCtx)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .load(product.getImage())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .into(holder.imageView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.textViewTitle.setText(product.getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.textViewShortDesc.setText(product.getShortdesc());&nbsp; &nbsp; &nbsp; &nbsp; holder.textViewtype.setText(String.valueOf(product.getType()));&nbsp; &nbsp; &nbsp; &nbsp; holder.textViewPrice.setText(String.valueOf(product.getPrice()));&nbsp; &nbsp; &nbsp; &nbsp; holder.ll.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent=new Intent(yourContext,YourActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent..putExtra("title",product.getTitle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return productList.size();&nbsp; &nbsp; }&nbsp; &nbsp; public class viewholder extends RecyclerView.ViewHolder{&nbsp; &nbsp; &nbsp; &nbsp; TextView textViewTitle, textViewShortDesc, textViewtype,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; textViewPrice;&nbsp; &nbsp; &nbsp; &nbsp; ImageView imageView;&nbsp; &nbsp; &nbsp; &nbsp; LinearLayout ll;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public viewholder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp;super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewTitle = itemView.findViewById(R.id.textViewTitle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewShortDesc =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;itemView.findViewById(R.id.textViewShortDesc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewtype = itemView.findViewById(R.id.textViewRating);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textViewPrice = itemView.findViewById(R.id.textViewPrice);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageView = itemView.findViewById(R.id.imageView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ll=itemView.findViewById(R.id.ll);// your layout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java