3 回答
TA贡献1865条经验 获得超7个赞
使用 Intent 传递值,如下所示:
@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);
}});
}
产品信息页面 Ativity 看起来像:
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");
}
}
产品信息_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>
TA贡献1853条经验 获得超9个赞
在你的 onCreateViewHolder 添加onClickListener到你的 itemView 的相关视图
@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;
}
TA贡献1830条经验 获得超3个赞
你可以用简单易行的方式做到这一点
定义列表项的布局并在其上设置点击侦听器。
使用意图将数据从回收器发送到您的活动。
公共类 adepter 扩展 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
}
}
}
添加回答
举报