我怎样才能获得cardview的信息,我点击它将其发送到下一个活动

时间:2016-05-26 14:53:45

标签: java android android-studio android-cardview

我正在制作一个应用程序,我使用recyclerview显示cardview列表,我希望能够进行用户点击的cardview的详细活动,但我不知道如何发送点击的项目下一个活动。

在这里你可以看到我的适配器:

/ **  *由alberto于18/05/16创建。  * /

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


}

2 个答案:

答案 0 :(得分:1)

将这些项目作为额外用于启动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);
}

答案 1 :(得分:1)

像这样更改你的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
    }
});