本文介绍了带有 ImageView 和不同活动的 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:一个 RecyclerView,上面有酒吧、咖啡店等地方的图片.

What I have: A RecyclerView with pictures of places like bars, cofee stores, etc.

我想要的是:当您点击其中一张图片时,我会向您显示所选地点的信息

What I want: That when you click on one of these images I show you the info of the selected place

我的问题:例如,如何为第三张图片设置 OnCLickListener?

My question: How can i set the OnCLickListener for the third picture for example?

许多人告诉我在onBindViewHolder()"方法上执行此操作,但它不允许我实现startActivity",因此我可以传递给Intent"拜托,如果你能用代码解释那就太好了,我还不是很好的编程,所以我真的很感激

Many people have told me to do it on my "onBindViewHolder()" method, but it doesn't allow me to implement the "startActivity" so i can pass to the "Intent" Please, if you can explain with code it would be great, I am not good programming yet, so I would really appreciate

我的适配器

public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolder> implements View.OnClickListener {

ArrayList<ClaseNueva> listalugares;
private View.OnClickListener listener;

public AdapterDatos(ArrayList<ClaseNueva> listaLugares) {
    listalugares = listaLugares;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//esto es lo que hacereferencia al xml donde vamos a meter la info
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false);//aqui le asignamos el valor del view al viewHolder
    view.setOnClickListener(this);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {// este metodo es el que se encarga de establecer la conexion entre el adaptador y la clase Viewholder ( a la cual le asignamos el xml)
    holder.etiLugares.setText(listalugares.get(position).getLugares());// asi se asignan los textos
    holder.Foto.setImageResource(listalugares.get(position).getFoto());//asi se asignan las fotos


}

@Override
public int getItemCount() {//este metodo va a decir el tamaño del viewHolder, en este caso de tamaño del array listalugares
    return listalugares.size();//se hace asi
}

public void setOnClickListener(View.OnClickListener listener){
    this.listener = listener;
}


@Override
public void onClick(View view) {


    if(listener!= null){
        listener.onClick(view);
   }

}

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView etiLugares;
    ImageView Foto;
    Context context;

    public ViewHolder(View itemView) {
        super(itemView);
        etiLugares = (TextView) itemView.findViewById(R.id.idNombre);//esto hace referencia a los elementos donde queremos meter la info
        Foto = (ImageView) itemView.findViewById(R.id.idImagen);

    }
}

}

我的 Java 课程

 public class foodAndGo extends Activity {

ArrayList<ClaseNueva> listalugares;
RecyclerView recyclerLugares;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_food_and_go);

    listalugares = new ArrayList<>();
    recyclerLugares = (RecyclerView) findViewById(R.id.RecyclerID);
    recyclerLugares.setLayoutManager(new LinearLayoutManager(this));


    llenarNombres();

    AdapterDatos adapter = new AdapterDatos(listalugares);
    adapter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Seleccion"+listalugares.get(recyclerLugares.getChildAdapterPosition(v)).getLugares(),Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(foodAndGo.this, MapsActivity.class);
            startActivity(myIntent);
        }
    });
  recyclerLugares.setAdapter(adapter);

}



private void llenarNombres() {
    listalugares.add(new ClaseNueva("Restaurantes", R.drawable.carnemejor));
    listalugares.add(new ClaseNueva("Bares", R.drawable.beers));
    listalugares.add(new ClaseNueva("Cafeterías",R.drawable.desayunosmejor));
    listalugares.add(new ClaseNueva("Pizzerias",R.drawable.pizzaamejor));
    listalugares.add(new ClaseNueva("Favoritos",R.drawable.favoritosmejo));


}

}

推荐答案

Inside foodAndGo 活动变化,

Inside foodAndGo activity change,

AdapterDatos adapter = new AdapterDatos(listalugares);AdapterDatos adapter = new AdapterDatos(listalugares,foodAndGo.this);

在适配器类中添加这个

private Context context;
public AdapterDatos(ArrayList<ClaseNueva> listaLugares,Context context) {
this.context=context;
listalugares = listaLugares;
}

现在,您可以像这样在适配器的 onBindViewHolder 中调用 startactivity

Now, you can call startactivity in adapter's onBindViewHolder like this

Intent myIntent = new Intent(context, MapsActivity.class);
context.startActivity(myIntent);

这篇关于带有 ImageView 和不同活动的 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:53