RecyclerView 问题中的 ImageButton

ImageButton in RecyclerView issue

我在 RecyclerView 中使用 ImageButton。我想在应用程序中处理一个ClickListener,这是ViewHolder的代码。

我正在使用 FirebaseUI,因此适配器是 FirebaseRecyclerAdapter。

RecyclerView 显示正确,但当我单击任何 ImageButton 时它没有执行任何操作。

怎么了?

这是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);
    }
}

我也试过像这样在 populateViewHolder() 中使用监听器:

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

}

最后,这是项目视图的XML:

<?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>

您好!

我相信一切正常,你只是忘了在 Toast 对象上调用 show()

Toast.makeText(..).show();