将RecyclerView放在Image前面

时间:2017-08-25 15:45:33

标签: android android-recyclerview android-cardview onitemclicklistener cardview

我正在构建一个使用RecyclerView和CardView的应用程序。由于RecyclerView没有onItemClickListener方法,我找到了一个实现此功能的类实现,并且能够在我的项目中插入此类。

在每张卡片中,我放了一个imageView(我使用Glide加载图片)和几个TextViews。当TextView位于图像下方时,我能够完美地使用onItemClickListener,但我决定将文本放在具有黑色透明背景的RelativeLayout内的图像前面。

问题是在此更改之后onItemClickListener停止工作。图像占据了整个cardView,TextView就在它的前面。通过单击这些元素中的任何一个,该方法都不起作用。我认为我需要将recyclelerView带到活动的前面,但我不知道该怎么做。我尝试使用recyclerView.bringToFront()但它没有用。

这是之前的卡片(单击白色区域时点击听众正在工作):

enter image description here

这就是现在的卡片(当我点击任何地方时,点击听众不会工作):

enter image description here

这是我的ItemClickSupport类:

public class ItemClickSupport {
    private final RecyclerView mRecyclerView;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
        }
    };
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mOnItemLongClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
            return false;
        }
    };
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener
            = new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnItemClickListener != null) {
                view.setOnClickListener(mOnClickListener);
            }
            if (mOnItemLongClickListener != null) {
                view.setOnLongClickListener(mOnLongClickListener);
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {

        }
    };

    private ItemClickSupport(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        mRecyclerView.setTag(R.id.item_click_support, this);
        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
    }

    public static ItemClickSupport addTo(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support == null) {
            support = new ItemClickSupport(view);
        }
        return support;
    }

    public static ItemClickSupport removeFrom(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support != null) {
            support.detach(view);
        }
        return support;
    }

    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        return this;
    }

    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
        return this;
    }

    private void detach(RecyclerView view) {
        view.removeOnChildAttachStateChangeListener(mAttachListener);
        view.setTag(R.id.item_click_support, null);
    }

    public interface OnItemClickListener {

        void onItemClicked(RecyclerView recyclerView, int position, View v);
    }

    public interface OnItemLongClickListener {

        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
    }
}

这是我从我的活动中调用它:

ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {

                Intent intent = new Intent(getBaseContext(), MovieDetails.class);
                intent.putExtra("movie_name", movies.get(position).getTitle());
                intent.putExtra("movie_rating", movies.get(position).getVoteAverage().toString());
                intent.putExtra("movie_description", movies.get(position).getOverview());
                intent.putExtra("movie_cover", movies.get(position).getPosterPath());
                intent.putExtra("movie_date", movies.get(position).getReleaseDate());
                intent.putExtra("movie_language", movies.get(position).getOriginalLanguage());
                intent.putExtra("movie_backdrop", movies.get(position).getBackdropPath());
                //intent.putExtra("album_name", albumList.get(position).getName());
                //intent.putExtra("album_thumbnail", Integer.toString(albumList.get(position).getThumbnail()));
                //intent.putExtra("number_songs", Integer.toString(albumList.get(position).getNumOfSongs()));

                startActivity(intent);
            }
        });

这是我的适配器:

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {

    private Context mContext;
    private List<MovieItem> movieList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, count;
        public ImageView thumbnail, overflow, star1, star2, star3, star4, star5;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            count = (TextView) view.findViewById(R.id.count);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
            star1 = (ImageView) view.findViewById(R.id.rating_image1);
            star2 = (ImageView) view.findViewById(R.id.rating_image2);
            star3 = (ImageView) view.findViewById(R.id.rating_image3);
            star4 = (ImageView) view.findViewById(R.id.rating_image4);
            star5 = (ImageView) view.findViewById(R.id.rating_image5);
            //overflow = (ImageView) view.findViewById(R.id.overflow);
        }
    }


    public MoviesAdapter(Context mContext, List<MovieItem> movieList) {
        this.mContext = mContext;
        this.movieList = movieList;
    }

    @Override
    public MoviesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.album_card, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MoviesAdapter.MyViewHolder holder, int position) {
        MovieItem movie = movieList.get(position);
        holder.title.setText(movie.getTitle());
        holder.count.setText("R: " + movie.getVoteAverage().toString() + "/10");

        Glide.with(mContext).load("https://image.tmdb.org/t/p/w500" + movie.getPosterPath()).into(holder.thumbnail);

    }


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

1 个答案:

答案 0 :(得分:0)

我设法通过做Sahil建议我做的事来解决我的问题。我将onClickListener放在图像而不是回收器视图上。为此,我必须将代码放在我的适配器中的onBindViewHolder方法中(我不知道我能做到这一点)。

这是这样的: (我必须将变量int position声明为final,以便在onClickListener中访问它并设置标志Intent.FLAG_ACTIVITY_NEW_TASK,因为我从Activity外部启动了另一个Activity。

 @Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

holder.thumbnail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, MovieDetails.class);
                intent.putExtra("movie_name", movieList.get(position).getTitle());
                intent.putExtra("movie_rating", movieList.get(position).getVoteAverage().toString());
                intent.putExtra("movie_description", movieList.get(position).getOverview());
                intent.putExtra("movie_cover", movieList.get(position).getPosterPath());
                intent.putExtra("movie_date", movieList.get(position).getReleaseDate());
                intent.putExtra("movie_language", movieList.get(position).getOriginalLanguage());
                intent.putExtra("movie_backdrop", movieList.get(position).getBackdropPath());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //intent.putExtra("album_name", albumList.get(position).getName());
                //intent.putExtra("album_thumbnail", Integer.toString(albumList.get(position).getThumbnail()));
                //intent.putExtra("number_songs", Integer.toString(albumList.get(position).getNumOfSongs()));

                mContext.startActivity(intent);
            }
        });
}