繁体   English   中英

我在 Horizontal Recycler View 上的 OnclickListener 没有按预期工作

[英]My OnclickListener on Horizontal Recycler View is not working as intended

我在 Horizontal Recycler View 上的 OnclickListener 未按预期工作。 它只会将第一个图标更新为存储在其下的图标。 但是当我单击回收站视图中的其他图标时它没有响应。 这是下面的代码

=

package com.example.vintage_cloapp_coursework.adapters;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vintage_cloapp_coursework.R;
import com.example.vintage_cloapp_coursework.models.HomeHorModel;
import com.example.vintage_cloapp_coursework.models.HomeVerModel;

import java.util.ArrayList;
import java.util.List;

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


    UpdateVerticalRec updateVerticalRec;
    Activity activity;
    ArrayList<HomeHorModel> list;
    Context context;

    boolean check = true;
    boolean select = true;
    int row_index = -1;

    public HomeHorAdapter(UpdateVerticalRec updateVerticalRec, Activity activity, ArrayList<HomeHorModel> list) {
        this.updateVerticalRec = updateVerticalRec;
        this.activity = activity;
        this.list = list;
    }



    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_horizontal_item,parent,false));




    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int adapterPosition = holder.getAdapterPosition();
        holder.imageView.setImageResource(list.get(adapterPosition).getImage());
        holder.name.setText(list.get(adapterPosition).getName());



        if (check){
            ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();
            homeVerModels.add(new HomeVerModel(R.drawable.stussy_jacket,"Stussy 8 Ball Jacket ","4.4", "$275"));
            homeVerModels.add(new HomeVerModel(R.drawable.f1_jacket,"Nascar Formula 1  Jacket ","3.9", "$75"));
            homeVerModels.add(new HomeVerModel(R.drawable.palm_angel_monclizzy,"Palm Angel Monclizzy","4.8", "$5000"));

            updateVerticalRec.callBack(adapterPosition, homeVerModels);
            check = false;
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    row_index = adapterPosition;
                    notifyDataSetChanged();

                    if(adapterPosition == 0){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();
                        homeVerModels.add(new HomeVerModel(R.drawable.stussy_jacket,"Stussy 8 Ball Jacket ","4.4", "$275"));
                        homeVerModels.add(new HomeVerModel(R.drawable.f1_jacket,"Nascar Formula 1  Jacket ","3.9", "$75"));
                        homeVerModels.add(new HomeVerModel(R.drawable.palm_angel_monclizzy,"Palm Angel Monclizzy","4.8", "$5000"));
                        homeVerModels.add(new HomeVerModel(R.drawable.band_tshirt,"Band Tshirt ","3.9", "$15"));


                        updateVerticalRec.callBack(adapterPosition,homeVerModels);
                    }
                    else if(adapterPosition == 1){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.slick_angel_tshirt," Slick Angel Tshirt","3.2", "$19"));
                        homeVerModels.add(new HomeVerModel(R.drawable.anime_girl_tshirt,"Anime Girl Tshirt ","4.4", "$27"));
                        homeVerModels.add(new HomeVerModel(R.drawable.band_tshirt,"Band Tshirt ","3.9", "$15"));

                        updateVerticalRec.callBack(adapterPosition,homeVerModels);
                    }
                    else if(adapterPosition == 2){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.star_shorts,"Star Shorts","4.8", "$50"));
                        updateVerticalRec.callBack(adapterPosition,homeVerModels);

                    }
                    else if(adapterPosition == 3){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.green_cargo_pants,"Green Cargo Pants","4.8", "$32"));
                        homeVerModels.add(new HomeVerModel(R.drawable.camo_cargo_pants,"Surplus Infantry Cargo Pants ","4.4", "$75"));

                        updateVerticalRec.callBack(adapterPosition,homeVerModels);

                    }
                    else if(adapterPosition == 4){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.black_trucker_hat,"Black Trucker Hat","4.8", "$21"));

                        updateVerticalRec.callBack(adapterPosition,homeVerModels);

                    }
                    else if(adapterPosition == 5){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.nb_350s_green,"Green NB 550s","4.8", "$120"));
                        homeVerModels.add(new HomeVerModel(R.drawable.blue_lv_shoes,"Louis Vuitton (Bleu Ciel)","3.9", "$1500"));

                        updateVerticalRec.callBack(adapterPosition,homeVerModels);

                    }
                    else if(adapterPosition == 6){
                        ArrayList<HomeVerModel> homeVerModels = new ArrayList<>();

                        homeVerModels.add(new HomeVerModel(R.drawable.brown_nike_sling_bag,"Brown Nike Sling Bag","4.8", "$5000"));

                        updateVerticalRec.callBack(adapterPosition,homeVerModels);
                    }
                }
            });
            if (select){
                if (adapterPosition == 0 ){
                    holder.cardView.setBackgroundResource(R.drawable.change_bg);
                }

            }else {
                if (row_index == adapterPosition){
                    holder.cardView.setBackgroundResource(R.drawable.change_bg);
                }else {
                    holder.cardView.setBackgroundResource(R.drawable.defualt_bg);

                }

            }
        }


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView name;
        CardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.hor_img);
            name =  itemView.findViewById(R.id.hor_text);
            cardView =  itemView.findViewById(R.id.cardView);


        }
    }
}

我收到一个关于 static 个位置的错误,我通过添加一个名为

adapterPosition

好像是boolean变量check问题。 第一次点击的时候是false,之后没有改成true,所以不会进入设置onClickListener的部分。

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM