android - RecyclerView 问题中的 ImageButton

标签 android firebase android-imageview firebaseui

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

}

最后,这是项目 View 的 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();

关于android - RecyclerView 问题中的 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41124541/

相关文章:

android - ImageView 显示灰色方 block 而不是图像

android - 我找不到 debug.keystore 文件

swift - 使用 Swift 根据条件从 Firebase 数据库中获取数据

javascript - Firebase 3 - javascript - 登录后获取facebook accessToken

node.js - Nodejs Firebase 和 Angular4 身份验证

android - 无法将 TouchImageView 与 ViewPager 一起使用

android - Fabric 插件与 Gradle 4.4 + 不兼容

android - Phonegap 应用程序 : Swipe from the screen edge

java - 关于android内部app文件的使用问题

android - 如何以编程方式将 9 补丁图像重新应用到 ImageView?