I'm using an ImageButton in a RecyclerView. I want to handle a ClickListener in the application, this is the code of the ViewHolder.
I'm using FirebaseUI so the adapter is a FirebaseRecyclerAdapter.
The RecyclerView is displayed correctly, but it doesn't do anything when I click any ImageButton.
What's wrong with it?
This is the code of viewHolder:
public class PicturesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Context context;
ImageButton imageButtonView;
TextView textIDView;
TextView latitudeView;
TextView longitudeView;
TextView dateView;
public PicturesHolder(View itemView) {
super(itemView);
this.context = itemView.getContext();
imageButtonView = (ImageButton) itemView.findViewById(R.id.image);
textIDView = (TextView)itemView.findViewById(R.id.texto);
latitudeView=(TextView) itemView.findViewById(R.id.latitude);
longitudeView = (TextView) itemView.findViewById(R.id.longitude);
dateView = (TextView) itemView.findViewById(R.id.dateView);
itemView.setOnClickListener(this);
imageButtonView.setOnClickListener(this);
}
public void setImage(String url) {
Glide.with(context)
.load(url)
.into(imageButtonView);
}
public void setTextIDView(String textID) {
textIDView.setText(textID);
}
public void setLatitude(double latitude)
{
latitudeView.setText(String.valueOf(latitude));
}
public void setLongitude(double longitude)
{
longitudeView.setText(String.valueOf(longitude));
}
public void setDateView(String date)
{
dateView.setText(date);
}
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Item Pressed = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT);
}
}
I also tried to use the listener inside populateViewHolder()
like this:
public void onAttachRecyclerView() {
//Query lastHundred = mURLReference.limitToLast(100);
Query mQuery = mURLReference.orderByChild("date").limitToLast(100);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Pictures, PicturesHolder>(
Pictures.class,R.layout.item_row,PicturesHolder.class, mQuery
) {
@Override
protected void populateViewHolder(PicturesHolder viewHolder, Pictures model, final int position) {
viewHolder.setTextIDView(model.getPictureID());
viewHolder.setImage(model.getDownloadURL());
viewHolder.setLatitude(model.getLatitude());
viewHolder.setLongitude(model.getLongitude());
viewHolder.setDateView(model.getDate());
viewHolder.imageButtonView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(view.getContext(), "You clicked on " + position, Toast.LENGTH_SHORT);
}
});
}
};
mImagesRV.setAdapter(mRecyclerViewAdapter);
}
Finally, this is the XML of the item view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="0.5">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/texto"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:src="@color/tw__composer_deep_gray"
android:layout_gravity="center"
android:padding="20dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_height="300dp"
android:layout_width="300dp"
android:id="@+id/image"
android:layout_weight="0.39" />
<TextView
android:text="Coordenadas:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/latitude"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/longitude"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/espacioBlanco"
android:text=" "/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dateView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/orderNumber"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
</android.support.v7.widget.CardView>
</LinearLayout>
Greetings!
I believe everything works as it should, you just forgot to call show() on Toast object
Toast.makeText(..).show();