Java recyclerview单击转到新页面android page info.xml并发送产品标题
recyclerview单击转到新页面android page info.xml并发送产品标题 我正在尝试创建一个电子商务应用程序 如何在RecyclerView列表中添加onclick打开产品信息页面,如新活动,我还想在产品信息页面中发送产品标题和IDJava recyclerview单击转到新页面android page info.xml并发送产品标题,java,android,android-recyclerview,Java,Android,Android Recyclerview,recyclerview单击转到新页面android page info.xml并发送产品标题 我正在尝试创建一个电子商务应用程序 如何在RecyclerView列表中添加onclick打开产品信息页面,如新活动,我还想在产品信息页面中发送产品标题和ID public class adepter extends RecyclerView.Adapter<adepter.viewholder> { private Context mCtx; private List&
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);
}
}
}
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");
}
}
公共类adepter扩展了RecyclerView.Adapter在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;
}
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");
}
}
你可以用简单易行的方法来做
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");
}
}
定义列表项的布局并在其上设置click listener
使用意图将数据从回收商发送到您的活动
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");
}
}
公共类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
}
}
}
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");
}
}
专用上下文mCtx;
私有列表产品列表;
公共adepter(上下文mCtx,列表productList){
this.mCtx=mCtx;
this.productList=productList;
}
@非空
@凌驾
public viewholder onCreateViewHolder(@NonNull ViewGroup parent,int
视图类型){
LayoutFlater充气机=LayoutFlater.from(mCtx);
视图v=充气机。充气(R.layout.product_list,父项,false);
返回新的视图持有者(v);
}
@凌驾
public void onBindViewHolder(@NonNull viewholder,int
(职位){
产品=产品列表。获取(位置);
//加载图像
滑翔带(mCtx)
.load(product.getImage())
.插入(支架.图像视图);
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(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(yourContext,YourActivity.class);
intent..putExtra(“title”,product.getTitle);
背景。开始触觉(意图);
}
});
}
@凌驾
public int getItemCount(){
返回productList.size();
}
公共类viewholder扩展了RecyclerView.viewholder{
TextView textViewTitle、textViewShortDesc、textViewtype、,
textViewPrice;
图像视图图像视图;
线性布局;
公共视图持有者(@NonNull View 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.findviewbyd(R.id.ll);//您的布局
}
}
}
使用意图传递值,如下所示:
@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");
}
}
产品信息页面似乎是:
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:
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");
}
}
<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>
我只需要tital.setText(产品);但它不起作用``TextView tital=findviewbyd(R.id.tital);product_id=getIntent().getStringExtra(“id”);product_tile=getIntent().getStringExtra(“标题”);tital.setText(产品);Toast.makeText(这个,product\u id,Toast.LENGTH\u SHORT.show();`和``Intent Intent=newintent(mCtx,product_info.class);intent.putExtra(“id”,product.getId());intent.putExtra(“title”,product.getTitle());mCtx.startActivity(intent);`公共类product_info扩展了AppCompativeActivity{String product_id,product_tile;TextView id,tital;@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layou.activity_product_info);TextView tital=findviewbyd(R.id.tital);product_id=getIntent().getStringExtra(“id”);product_tile=getIntent().getStringExtra(“title”);tital.setText(product_tile);Toast.makeText(this,product_id,Toast.LENGTH_SHORT)。show();}`@Aamir Shahzadtital.setText(product_tile);这里出了什么问题?请注意,将OnClickListener放在onBindViewHolder中不是一个好主意,让内部类来执行click listener。