我有一个cardview的回收视图,每个cardview都有一个竖起大拇指(如)按钮。
我希望当用户点击特定卡片上的拇指按钮时,他们“喜欢”该帖子,按钮动画并处理相应的逻辑。
到目前为止,这是我的代码:
1 2 3 4 5 6
1 2639 156 69 49 21 42
2 150 300 53 11 1 1
3 74 20 363 48 8 4
4 60 7 46 384 65 12
5 42 2 5 75 275 63
6 40 0 1 15 52 402
library(markovchain)
mcobject<-as(mytableX, "markovchain")
1 2 3 4 5 6
1 0.88676075 0.052419355 0.023185484 0.01646505 0.007056452 0.014112903
2 0.29069767 0.581395349 0.102713178 0.02131783 0.001937984 0.001937984
3 0.14313346 0.038684720 0.702127660 0.09284333 0.015473888 0.007736944
4 0.10452962 0.012195122 0.080139373 0.66898955 0.113240418 0.020905923
5 0.09090909 0.004329004 0.010822511 0.16233766 0.595238095 0.136363636
6 0.07843137 0.000000000 0.001960784 0.02941176 0.101960784 0.788235294
当用户点击like按钮时,它会在toast中显示正确的适配器位置,但它没有像它应该的那样动画,我无法在其上设置动画。通常情况下,按钮在单击时会动画,但是当它在Recyclerview中时,它似乎不起作用。
我试着做
private class CardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CardView mCardView;
private TextView cardTitle, cardUsername;
private ImageView cardImage;
public CardViewHolder(View itemView){
super(itemView);
mCardView = (CardView) itemView.findViewById(R.id.cardview);
cardTitle = (TextView) itemView.findViewById(R.id.cardTitle);
cardUsername = (TextView) itemView.findViewById(R.id.cardUsername);
cardImage = (ImageView) itemView.findViewById(R.id.cardImage);
mLikeButton = (LikeButton) itemView.findViewById(R.id.thumb);
itemView.setOnClickListener(this);
mLikeButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == mLikeButton.getId()){
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(), mRecipes[getAdapterPosition()].getRecipename().toString(), Toast.LENGTH_SHORT).show();
}
}
}
但演员似乎不起作用。我希望能够直接引用recyclerview特定行中的特定like按钮,以便我可以在其上调用方法。这可能吗?
这是在按钮上设置点击监听器的不好方法吗?
答案 0 :(得分:0)
问题在于视图ID检查。如果您想使用您的解决方案,那将是这样的:
private static class CardViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private CardView mCardView;
private TextView cardTitle, cardUsername;
private ImageView cardImage;
public CardViewHolder(View itemView) {
super(itemView);
mCardView = (CardView) itemView.findViewById(R.id.cardview);
cardTitle = (TextView) itemView.findViewById(R.id.cardTitle);
cardUsername = (TextView) itemView.findViewById(R.id.cardUsername);
cardImage = (ImageView) itemView.findViewById(R.id.cardImage);
mLikeButton = (LikeButton) itemView.findViewById(R.id.thumb);
itemView.setOnClickListener(this);
mLikeButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v instanceof LikeButton) {
// You now this is the LikeButton
Toast.makeText(v.getContext(), "ITEM PRESSED = " +
String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
((LikeButton) v).setLiked(true);
} else {
// Not a LikeButton
Toast.makeText(v.getContext(), "ROW PRESSED = " +
String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(),
mRecipes[getAdapterPosition()].getRecipename().toString(),
Toast.LENGTH_SHORT).show();
}
}
}
我们在这里做的是检查View
是否为LikeButton
。如果是这样,我们可以安全地投射它并在其上调用我们的方法。如果不是itemView
。
在我看来,更好的方法是匿名实施OnClickListener
。这看起来像这样:
private static class CardViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private CardView mCardView;
private TextView cardTitle, cardUsername;
private ImageView cardImage;
public CardViewHolder(View itemView) {
super(itemView);
mCardView = (CardView) itemView.findViewById(R.id.cardview);
cardTitle = (TextView) itemView.findViewById(R.id.cardTitle);
cardUsername = (TextView) itemView.findViewById(R.id.cardUsername);
cardImage = (ImageView) itemView.findViewById(R.id.cardImage);
mLikeButton = (LikeButton) itemView.findViewById(R.id.thumb);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// You now for sure this is an ItemView.
Toast.makeText(v.getContext(), "ROW PRESSED = " +
String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT)
.show();
Toast.makeText(v.getContext(),
mRecipes[getAdapterPosition()].getRecipename().toString(),
Toast.LENGTH_SHORT).show();
}
});
mLikeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// You now for sure this is a LikeButton.
Toast.makeText(v.getContext(), "ITEM PRESSED = " +
String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT)
.show();
((LikeButton) v).setLiked(true);
}
});
}
}
我们想要处理的每个OnClickListener
都有一个View
。因为每个View
都有自己的View
,所以我们始终知道哪个HostingEnvironment.QueueBackgroundWorkItem
已被点击,并且可以采取相应的行动。