RecyclerView的onClick无效,仅当您单击以下项目之间时,该项目才有效:已解决

时间:2019-10-08 09:02:48

标签: android android-recyclerview

我正在学习Android,并且正在使用RecyclerView进行测试。我想添加一个OnItemClickListener,我想看看是否可行,所以每次单击项目时都会出现一个Toast,但是有些奇怪的事情发生,仅在单击“回收站”卡之间时触发,而在单击项目时不触发。我一直在寻找有同样问题的人,但没有发现任何东西。

这是RecyclerAdapter及其活动。

适配器

public class CardAdapter extends ListAdapter<Card, CardAdapter.CardHolder> {

    private OnItemClickListener listener;
    private Context mContext;
    private Intent myIntent;

    protected CardAdapter() {

        super(DIFF_CALLBACK);
    }

    private static final DiffUtil.ItemCallback<Card> DIFF_CALLBACK = new DiffUtil.ItemCallback<Card>() {
        @Override
        public boolean areItemsTheSame(Card oldItem, Card newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(Card oldItem, Card newItem) {
            return oldItem.getCardImage() == newItem.getCardImage() &&
                    oldItem.getCardName().equals(newItem.getCardName()) &&
                    oldItem.getCardCode().equals(newItem.getCardCode()) &&
                    oldItem.getCardSet().equals(newItem.getCardSet()) &&
                    oldItem.getQuantity() == newItem.getQuantity() &&
                    oldItem.getRarity().equals(newItem.getRarity());

        }
    };

    @NonNull
    @Override
    public CardHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item, parent, false);
        mContext = parent.getContext();
        return new CardHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CardHolder holder, int position) {
        Card currentCard = getItem(position);
        Picasso.with(mContext).load(currentCard.getCardImage()).fit().centerInside().into(holder.cardImage);
        holder.cardName.setText(currentCard.getCardName());
        holder.cardCode.setText(currentCard.getCardCode());
        holder.cardSet.setText(currentCard.getCardSet());
        holder.rarity.setText(currentCard.getRarity());


    }


    class CardHolder extends RecyclerView.ViewHolder {

        private ImageView cardImage;
        private TextView cardName;
        private TextView cardCode;
        private TextView cardSet;
        private TextView quantity;
        private TextView rarity;

        public CardHolder(View itemView) {
            super(itemView);
            cardImage = itemView.findViewById(R.id.cardImage);
            cardName = itemView.findViewById(R.id.cardName);
            cardCode = itemView.findViewById(R.id.cardCode);
            cardSet = itemView.findViewById(R.id.cardSet);
            rarity = itemView.findViewById(R.id.cardRarity);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(mContext, "epaepa", Toast.LENGTH_SHORT).show();
                    Log.d("LogDiaViewHolder","epaepa");

                }
            });
        }

        public ImageView getCardImage() {
            return cardImage;
        }

    }

    public interface OnItemClickListener {
        void onItemClick(Card card);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}

活动

public class CardListActivity extends AppCompatActivity {

    private CardViewModel cardViewModel;
    private View decorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final CardAdapter adapter = new CardAdapter();
        recyclerView.setAdapter(adapter);

        cardViewModel = ViewModelProviders.of(this).get(CardViewModel.class);
        cardViewModel.getAllCards().observe(this, new Observer<List<Card>>() {
            @Override
            public void onChanged(List<Card> cards) {
                adapter.submitList(cards);
            }
        });

        decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if (visibility == 0) {
                    decorView.setSystemUiVisibility(hideSystemBars());
                }
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            decorView.setSystemUiVisibility(hideSystemBars());
        }
    }

}

如果有人想要完整的项目,我会在这里留下仓库:https://github.com/nochuckles/BattleSpiritsCBDB

任何帮助都会很好,谢谢!

3 个答案:

答案 0 :(得分:2)

我修复了!显然,如果您具有以下属性:

clickable:true

在您的CardView上会发生这种情况。

我不知道为什么,我正在测试不同的东西,当我删除它时,它开始可以正常工作。我不知道为什么会这样,我会做一些研究,但是如果有人对此有一些了解,我将非常感谢,谢谢!

答案 1 :(得分:0)

BindViewHolder

中设置您的OnClickListeners
@Override
    public void onBindViewHolder(@NonNull CardHolder holder, int position) {
        Card currentCard = getItem(position);
        Picasso.with(mContext).load(currentCard.getCardImage()).fit().centerInside().into(holder.cardImage);
        holder.cardName.setText(currentCard.getCardName());
        holder.cardCode.setText(currentCard.getCardCode());
        holder.cardSet.setText(currentCard.getCardSet());
        holder.rarity.setText(currentCard.getRarity());

        //example
        holder.cardName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(mContext, "epaepa", Toast.LENGTH_SHORT).show();
                Log.d("LogDiaViewHolder","epaepa");

            }
        });
}

答案 2 :(得分:0)

显示Toast并不是您的适配器,您必须使用接口OnItemClickListener来在项目被点击时通知您的活动。为此,您的活动必须实现此接口和onItemClick方法:

public class CardListActivity extends AppCompatActivity implements OnItemClickListener {
    ....
    void onItemClick(Card card) {
         Toast.makeText(mContext, "epaepa", Toast.LENGTH_SHORT).show();
                Log.d("LogDiaViewHolder","epaepa");
    }
}

要在适配器中启动onItemClick事件,必须将接口的实例传递给适配器

public class CardAdapter extends ListAdapter<Card, CardAdapter.CardHolder> {
    //....
    private OnItemClickListener mListener

    protected CardAdapter(OnItemClickListener listener) {
        super(DIFF_CALLBACK);
        this.mListener = listener;
    }
}

然后在项目单击时调用接口方法

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 mListener.onItemClick(v)
            }
        });