gpt4 book ai didi

android - 如何保持 RecyclerView 的按钮被恭敬地点击 - Android

转载 作者:太空宇宙 更新时间:2023-11-03 13:19:51 25 4
gpt4 key购买 nike

我有一个填充了 CardView 的 RecyclerView。在每个 CardViews 上都有一个按钮,用于对帖子进行投票。

这是按钮未按下时的样子,

up vote - not pressed

这是按下按钮时的样子,

up vote - pressed

我的代码可以实现这一点,但我遇到了一个问题,因为它是一个 RecyclerView。当我向下滚动帖子时,RecyclerView 会回收之前投票过的帖子。因此,即使用户从未投票过,帖子也会显示它已投票。

如何让每个 CardView 的按钮保持按下状态?

这是我的适配器

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

private String[] mDataset;

Typeface customFont;

// 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 {
public TextView mTitle;
public TextView mVoterCounter;
public ImageButton mVoterButton;
public ViewHolder(android.support.v7.widget.CardView v) {
super(v);
mTitle = (TextView) v.findViewById(R.id.title);
mVoterCounter = (TextView) v.findViewById(R.id.voter_counter);

//Initialize voter button
mVoterButton = (ImageButton)v.findViewById(R.id.voter);

mVoterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
}
});
}
}

// Provide a suitable constructor (depends on the kind of dataset)
public DiscoverRecyclerAdapter(String[] myDataset, Typeface passedFont) {
mDataset = myDataset;
customFont = passedFont;
}

// Create new views (invoked by the layout manager)
@Override
public DiscoverRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_discover, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder((android.support.v7.widget.CardView)v);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTitle.setText(mDataset[position]);
holder.mTitle.setTypeface(customFont);
holder.mVoterCounter.setTypeface(customFont);
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}

最佳答案

除了 mDataset,您还需要一个 bool 数组,比如 mIsSelected现在它的大小将等于数组 mDataSet 的大小,或者如果需要,可以创建类。

然后在 onBindViewHolder 中做

 if(mIsSelected[position]
mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
else
mVoterButton.setImageResource(R.drawable.ic_voter_unpressed);

并将按钮 onclick 移动到 onBindViewHolder 中,如下所示

 holder.mVoterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
mIsSelected[position] = true;
}
});

关于android - 如何保持 RecyclerView 的按钮被恭敬地点击 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30127720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com