3 回答

TA貢獻1865條經(jīng)驗 獲得超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);
}});
}
產(chǎn)品信息頁面 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");
}
}
產(chǎn)品信息_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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超3個贊
你可以用簡單易行的方式做到這一點
定義列表項的布局并在其上設置點擊偵聽器。
使用意圖將數(shù)據(jù)從回收器發(fā)送到您的活動。
公共類 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
}
}
}
添加回答
舉報