Нажмите ссылку, чтобы перейти на новую страницу android page info.xml и отправьте название продукта. - PullRequest
0 голосов
/ 07 октября 2019

recyclerview нажмите, перейдите на новую страницу android page info.xml и отправьте название продукта. Я пытаюсь создать приложение для электронной коммерции. Как добавить onclick в список RecyclerView, открыть страницу с информацией о продукте, например, новую активность, я также хотел бы отправитьНазвание и идентификатор продукта на странице информации о продукте,

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


}

enter image description here

Ответы [ 3 ]

0 голосов
/ 07 октября 2019

Вы можете сделать это простым и легким способом

  1. Определите макет для вашего элемента списка и установите прослушиватель щелчка по нему.
  2. Используйте намерение для отправки данных из переработчикав вашу деятельность.

    адептер публичного класса расширяет RecyclerView.Adapter {

        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()));
            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(yourContext,YourActivity.class);
                    intent..putExtra("title",product.getTitle);
                    context.startActivity(intent);
    
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return productList.size();
        }
    
    
        public class viewholder extends RecyclerView.ViewHolder{
            TextView textViewTitle, textViewShortDesc, textViewtype, 
            textViewPrice;
            ImageView imageView;
            LinearLayout ll;
    
                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);
                ll=itemView.findViewById(R.id.ll);// your layout
                 }
         }
    
    
    }
    
0 голосов
/ 07 октября 2019

Используйте Intents для передачи значений, как показано ниже:

@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.textViewPrice.setText(String.valueOf(product.getPrice()));
    final Product p=product ;
    holder.itemView.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    Intent intent=new Intent(mCtx,InfoPage.class);
    intent.putExtra("prod_id",p.getID());
    intent.putExtra("prod_title",p.getTitle());
    startActivity(intent);

}});

}

Активность страницы с информацией о продукте выглядит следующим образом:

public class InfoPage extends AppCompatActivity
{
    WebView webview;
    String product_id,product_tile;
    protected void onCreate(Bundle savedInstanceState) 
    {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.product_info_xml);
      product_id=getIntent().getStringExtra("prod_id");
      product_tile=getIntent().getStringExtra("prod_title");
      getSupportActionBar().setTitle(product_tile);
      webview=findViewbyID(R.id.myWebView);
      webview.loadUrl("http://www.YourProductUrl.com");
    }
}

product_info_xml:

<LinearLayout 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">

 <WebViewView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
0 голосов
/ 07 октября 2019

в вашем onCreateViewHolder добавьте onClickListener к соответствующему представлению вашего элемента ViewView

 @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);
    viewholder vh = new viewholder(v);
    vh.itemView.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
          Product product = productList.get(vh.getAdapterPosition());
          //do the page opening here

       }
    });
    return vh;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...