我的问题,当我点击我的 ImageView
时,我的 CardView
展开了,但是第 n 个 CardView
也展开了,这需要我单击的展开的 CardView
。我不明白为什么我的 onClick
方法也应用于其他 CardView
。
我的适配器
public class CardsViewAdapter extends RecyclerView.Adapter<CardsViewAdapter.ViewHolder> {
private Game[] mDataset;
int rotationAngle = 0;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ImageView imageView;
public LinearLayout test2;
public TextView test3;
boolean isPopupVisible;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text_cards);
imageView = (ImageView) v.findViewById(R.id.item_description_game_more);
test2 = (LinearLayout) v.findViewById(R.id.popup_layout);
test3 = (TextView) v.findViewById(R.id.test_view);
isPopupVisible = false;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public CardsViewAdapter(Game[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_resume_game, parent, false);
// set the view's size, margins, paddings and layout parameters
//...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
//TODO : complete
final int pos = position;
holder.mTextView.setText(String.valueOf(mDataset[position].getId_game()));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("POS","actual pos = "+pos);
holder.test3.setText("Position : "+pos);
// Perform action on click
if (holder.isPopupVisible) {
holder.isPopupVisible = false;
ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
anim.setDuration(500);
anim.start();
rotationAngle += 180;
rotationAngle = rotationAngle%360;
CardsAnimationHelper.collapse(holder.test2);
} else {
holder.isPopupVisible = true;
ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
anim.setDuration(500);
anim.start();
rotationAngle += 180;
rotationAngle = rotationAngle%360;
CardsAnimationHelper.expand(holder.test2);
}
}
});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
最佳答案
我认为您已经有了Game
类。只需在该类中添加一个名为 expanded
的新属性。
public Class Game {
public int id;
public String description;
// Add an extra attribute to track the checked status
// By default, set the checked status to false
public boolean expanded = false;
}
现在您有了一个跟踪展开状态的属性,您可以轻松地执行类似的操作。
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final int pos = position;
holder.mTextView.setText(String.valueOf(mDataset[position].id));
// Set the expanded or collapsed mode here
if(mDataset[position].expanded) expandView(holder.test2);
else collapseView(holder.test2);
// Now set the onClickListener like this
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.test3.setText("Position : "+pos);
// Animate the imageView here
animateImageView(holder.imageView);
// Toggle the expanded attribute value
if(mDataset[position].expanded) mDataset[position].expanded = false;
else mDataset[position].expanded = true;
// Now call notifyDataSetChanged to make the change to effect
refreshList();
}
});
}
// Extra functions inside your adapter class to improve readability
private void collapseView(View view) {
CardsAnimationHelper.collapse(view);
}
private void expandView(View view) {
CardsAnimationHelper.expand(view);
}
private void animateImageView(ImageView imageView) {
ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "rotation",rotationAngle, rotationAngle + 180);
anim.setDuration(animationDuration);
anim.start();
rotationAngle += 180;
rotationAngle = rotationAngle % 360;
}
long animationDuration = 500;
private void refreshList() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
}, animationDuration);
}
关于android - 如何在 RecyclerView 中的 CardView 上使用 OnClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475119/