RecyclerView onClick无法正常工作? [英] RecyclerView onClick not working properly?

查看:78
本文介绍了RecyclerView onClick无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在片段中使用RecyclerView来显示带有Grid格式文本的图像,Recycler视图grid_item.xml如下所示:

I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin_grid"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageView
                android:id="@id/thumbnail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:background="?selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitCenter" />

            <TextView
                android:id="@id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:layout_margin="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

我正在使用适配器在RecyclerView中填充数据,其代码为:

I am using adapter to populate data in RecyclerView, the code for this is :

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

    private ArrayList<Movie> movies;
    private Context context;

    public DataAdapter(Context context, ArrayList<Movie> movies) {
        this.movies = movies;
        this.context = context;
    }

    public void addItems(ArrayList<Movie> movies) {

        this.movies.addAll(movies);

    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

        viewHolder.title.setText(movies.get(i).getTitle());
        Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
    }

    @Override
    public int getItemCount() {
        return movies.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView title;
        private ImageView thumbnail;

        public ViewHolder(View view) {
            super(view);

            title = (TextView) view.findViewById(R.id.title);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
            view.setOnClickListener(this);
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View view) {
            int position = getAdapterPosition(); // gets item position
            if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                Movie movie = movies.get(position);
                // start detail activity
                Intent i = new Intent(context, MovieDetail.class);
                i.putExtra(Constants.MOVIES_OBJECT, movie);
                context.startActivity(i);
            }
        }
    }


}

我的问题是单击侦听器仅在TextView而不是图像上起作用,尽管我在包含图像和文本的整个视图上设置了单击侦听器.我的实现有问题吗?

My problem is click listener is only working on the TextView and not on the image , although I have set click listener on whole view which contains both image and text.Is something wrong with my implementation ?

推荐答案

尝试将android:focusableInTouchMode="false"设置为您的imageview. 并删除android:clickable="true"或将其设置为false

try setting android:focusableInTouchMode="false" to your imageview. and remove android:clickable="true" or set it to false

这篇关于RecyclerView onClick无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆