Toast 消息 onClick NOT SHOWING (RecycleView)

Toast message onClick NOT SHOWING (RecycleView)

我尝试在单击 RecycleView 中的某个项目时显示祝酒消息,我已经尝试了很多示例, 但它没有给我任何东西。有人可以给我一个我可以遵循的不同示例,最后我想设置 onClick 以显示一个新片段。如果我能得到一个例子,那就太好了。 我正在使用此代码:

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

    private List<Movie> movies;
    private int card_layout;
    private Context mContext;

    public MovieAdapter(List<Movie> movies, int card_layout, Context context) {

        this.movies = movies;
        this.card_layout = card_layout;
        this.mContext = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(card_layout, viewGroup, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        final Movie movie = movies.get(i);

        viewHolder.movieImage.setImageDrawable(mContext.getDrawable(movie.getImageResourceId(mContext)));
        viewHolder.movieName.setText(movie.mName);

        viewHolder.currentMovie = movie;

    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView movieName;
        public ImageView movieImage;
        public Movie currentMovie;


        public ViewHolder( View itemView) {
            super(itemView);
            movieName = (TextView) itemView.findViewById(R.id.movieName);
            movieImage = (ImageView)itemView.findViewById(R.id.movieImage);


            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
            public void onClick(View itemView){
                    Toast.makeText(itemView.getContext(),currentMovie.mName,Toast.LENGTH_SHORT ).show();
                }
            });
        }

    }
}

我是否也必须在我的 MainActivity 中实现一些东西? 请不要生我的气,我只是这一切的开始。我们将感谢您的所有帮助。谢谢

布局默认是不可点击的。使点击添加 setClickabletrue :

View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
                                                 card_layout, viewGroup, false);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);

在浪费了一个小时的时间后,我找到了这个问题最合适最简单的解决方案:

试试这个,肯定会 work.No 是否在网格中使用卡片。

RecyclerView 适配器:

ProductCardRecyclerViewAdapter.java

public class ProductCardRecyclerViewAdapter extends RecyclerView.Adapter<ProductCardViewHolder> {

    public final String TAG=getClass().getSimpleName();
    Context context;
    private List<ProductEntry> productList;
    private Integer[] cardImages;
    String[] cardTitle;
    String[] cardSubtitle;

    public ProductCardRecyclerViewAdapter(Context context, Integer[] imageList, String[] cardTitle, String[] cardSubtitle) {
        this.cardImages = imageList;
        this.cardTitle = cardTitle;
        this.cardSubtitle = cardSubtitle;
        this.context = context;

    }

    @NonNull
    @Override
    public ProductCardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card, parent, false);

        return new ProductCardViewHolder(layoutView);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductCardViewHolder holder, int position) {
        // TODO: Put Recycler ViewHolder Cards binding code here in MDC-102
        holder.imgCard.setImageResource(cardImages[position]);
        holder.productTitle.setText(cardTitle[position]);
        holder.productPrice.setText(cardSubtitle[position]);
        holder.productCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: Material Card clicked "+cardTitle[position]+" : "+context.getClass());

                //TODO: Perform card clicked working
                Context c = v.getContext();
                Toast.makeText(c, cardTitle[position], Toast.LENGTH_SHORT).show();
            }
        });

    }
    

    @Override
    public int getItemCount() {
        return cardImages.length;
    }
}

RecyclerViewHolder

ProductCardViewHolder.java


public class ProductCardViewHolder extends RecyclerView.ViewHolder  {

    CardView productCard;
    ImageView imgCard;
    public TextView productTitle;
    public TextView productPrice;

    public ProductCardViewHolder(@NonNull View itemView) {
        super(itemView);
        imgCard = itemView.findViewById(R.id.product_image);
        productTitle = itemView.findViewById(R.id.product_title);
        productPrice = itemView.findViewById(R.id.product_price);
        productCard=itemView.findViewById(R.id.cardofproducts);
        // TODO: Find and store views from itemView
    }


}

希望,对你有很大帮助。