我怎样才能得到我点击发送到下一个的cardview的信息activity
How can i get the info of the cardview i click on to send it to the next activity
我正在制作一个应用程序,我使用 recyclerview 显示卡片视图列表,我希望能够制作用户点击的卡片视图的详细信息 activity,但我不知道如何发送单击的项目到下一个 activity.
在这里你可以看到我的适配器:
/**
* 由阿尔贝托于 2016 年 5 月 18 日创建。
*/
public class EmpresasAdapter extends RecyclerView.Adapter<EmpresasAdapter.EmpresasViewHolder>
implements ItemClickListener{
private final Context context;
private List<Empresas> items;
public EmpresasAdapter(Context context, List<Empresas> items) {
this.context = context;
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.empresa_card, viewGroup, false);
return new EmpresasViewHolder(v, this);
}
@Override
public void onBindViewHolder(EmpresasViewHolder viewHolder, int i) {
// Item procesado actualmente
Empresas currentItem = items.get(i);
viewHolder.nombrep.setText(currentItem.getNombre());
viewHolder.descripcionp.setText(currentItem.getDescripcionC());
viewHolder.franquiciap.setText(currentItem.getModalidad()+" | "+currentItem.getFranquicia());
// Cargar imagen
Glide.with(context)
.load(currentItem.getImagen())
.into(viewHolder.imagenp);
}
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
EmpresaDetalle.launch(
(Activity) context, position, sharedImage);
}
/**
* View holder para reciclar elementos
*/
public static class EmpresasViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Views para un curso
public ImageView imagenp;
public TextView nombrep;
public TextView descripcionp;
public TextView franquiciap;
// Interfaz de comunicación
public ItemClickListener listener;
public EmpresasViewHolder(View v, ItemClickListener listener) {
super(v);
nombrep = (TextView) v.findViewById(R.id.nombrep);
descripcionp = (TextView) v.findViewById(R.id.descripcionp);
franquiciap = (TextView) v.findViewById(R.id.Franquiciap);
imagenp = (ImageView) v.findViewById(R.id.imagenp);
v.setOnClickListener(this);
this.listener = listener;
}
@Override
public void onClick(View v) {
listener.onItemClick(v, getAdapterPosition());
}
}
将项目作为启动 DetailedActivty 的额外内容:
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
Intent intent = new Intent(context, DetailedActivty.class)
intent.putExtra(nombrep, NOMBREP_EXTRA);
intent.putExtra(descripcionp, DESCRIPCIONP_EXTRA);
intent.putExtra(franquiciap, FRANQUICIAP_EXTRA);
context.startActivity(intent);
}
像这样更改您的 onCreateViewHolder
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empresa_card, viewGroup, false);
v.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//you have the cardView in v;
//you can access the child view of your cardView by referring to v
//if you want to get the position you can do it here.
//Create an intent with the collected data an d start the activity
}
});
return new EmpresasViewHolder(v, this);
}
您也可以在 onBindView()
方法中执行此操作。使用此方法可以轻松访问位置。但这并不意味着你不能在前一种方法中占有一席之地。你可以。但我没有描述它。想做就做。
//to do this its easier if you have assigned and id to the cardView in XML and store the cardView in the viewHOlder
//getting cardView
CardView cardView=holder.cardView;
cardView.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//position is easily availble now.
//you can access the rest of the views
//create youtr intent and start the activity
}
});
我正在制作一个应用程序,我使用 recyclerview 显示卡片视图列表,我希望能够制作用户点击的卡片视图的详细信息 activity,但我不知道如何发送单击的项目到下一个 activity.
在这里你可以看到我的适配器:
/** * 由阿尔贝托于 2016 年 5 月 18 日创建。 */
public class EmpresasAdapter extends RecyclerView.Adapter<EmpresasAdapter.EmpresasViewHolder>
implements ItemClickListener{
private final Context context;
private List<Empresas> items;
public EmpresasAdapter(Context context, List<Empresas> items) {
this.context = context;
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.empresa_card, viewGroup, false);
return new EmpresasViewHolder(v, this);
}
@Override
public void onBindViewHolder(EmpresasViewHolder viewHolder, int i) {
// Item procesado actualmente
Empresas currentItem = items.get(i);
viewHolder.nombrep.setText(currentItem.getNombre());
viewHolder.descripcionp.setText(currentItem.getDescripcionC());
viewHolder.franquiciap.setText(currentItem.getModalidad()+" | "+currentItem.getFranquicia());
// Cargar imagen
Glide.with(context)
.load(currentItem.getImagen())
.into(viewHolder.imagenp);
}
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
EmpresaDetalle.launch(
(Activity) context, position, sharedImage);
}
/**
* View holder para reciclar elementos
*/
public static class EmpresasViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Views para un curso
public ImageView imagenp;
public TextView nombrep;
public TextView descripcionp;
public TextView franquiciap;
// Interfaz de comunicación
public ItemClickListener listener;
public EmpresasViewHolder(View v, ItemClickListener listener) {
super(v);
nombrep = (TextView) v.findViewById(R.id.nombrep);
descripcionp = (TextView) v.findViewById(R.id.descripcionp);
franquiciap = (TextView) v.findViewById(R.id.Franquiciap);
imagenp = (ImageView) v.findViewById(R.id.imagenp);
v.setOnClickListener(this);
this.listener = listener;
}
@Override
public void onClick(View v) {
listener.onItemClick(v, getAdapterPosition());
}
}
将项目作为启动 DetailedActivty 的额外内容:
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
Intent intent = new Intent(context, DetailedActivty.class)
intent.putExtra(nombrep, NOMBREP_EXTRA);
intent.putExtra(descripcionp, DESCRIPCIONP_EXTRA);
intent.putExtra(franquiciap, FRANQUICIAP_EXTRA);
context.startActivity(intent);
}
像这样更改您的 onCreateViewHolder
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empresa_card, viewGroup, false);
v.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//you have the cardView in v;
//you can access the child view of your cardView by referring to v
//if you want to get the position you can do it here.
//Create an intent with the collected data an d start the activity
}
});
return new EmpresasViewHolder(v, this);
}
您也可以在 onBindView()
方法中执行此操作。使用此方法可以轻松访问位置。但这并不意味着你不能在前一种方法中占有一席之地。你可以。但我没有描述它。想做就做。
//to do this its easier if you have assigned and id to the cardView in XML and store the cardView in the viewHOlder
//getting cardView
CardView cardView=holder.cardView;
cardView.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//position is easily availble now.
//you can access the rest of the views
//create youtr intent and start the activity
}
});