我在下面的代码中遇到了一些奇怪的行为。我在ListView
的每一行中都有一个按钮,如果单击它,我想使该行变灰并隐藏该按钮。为了使更改永久生效,我在自定义ViewHolder
中设置了一个属性,以便更改是持久的。
但是,单击按钮,例如第1项,也会将更改应用到第6项。在其他项上,它不影响其他行或影响不止一行。我怀疑这与声明一些final
变量有关,但是我不确定。我尝试实现不同的解决方案(减少最终变量的数量,使用不同的变量等),但似乎没有任何效果。
我需要一些帮助。谢谢!
public class FruitLoopsAdapter extends BaseAdapter
{
private ArrayList<FruitLoopsNode> provider;
private Activity parent;
/**
* Constructor.
* @param parent
* The parent activity of the fragment using the FruitLoop. Needed as the adapter will
* be used in a fragment which only its parents has access to the data providers.
*/
public FruitLoopsAdapter(Activity parent)
{
this.parent = parent;
provider = ((Box)parent).getFruitLoops();
}
/**
* Method returning the size of the underlying data provider.
* @return
* The size of the data provider.
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount()
{
return provider.size();
}
/**{@inheritDoc}*/
@Override
public Object getItem(int position)
{
return provider.get(position);
}
/**{@inheritDoc}*/
@Override
public long getItemId(int position)
{
return position;
}
/**
* Method building the composite custom view and returning it as one view.
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parentViewGroup)
{
FruitLoopsViewHolder viewHolder;
final FruitLoopsNode FruitLoopsNode = provider.get(position);
if (convertView == null)
{
convertView = LayoutInflater.from(parentViewGroup.getContext()).inflate(R.layout.list_FruitLoops, null);
viewHolder = new FruitLoopsViewHolder(
(TextView) convertView.findViewById(R.id.time),
(Button) convertView.findViewById(R.id.consume),
(ImageView) convertView.findViewById(R.id.thumbnail),
(TextView) convertView.findViewById(R.id.content_title),
(TextView) convertView.findViewById(R.id.content_brief),
(RatingBar) convertView.findViewById(R.id.ratingBar));
convertView.setTag(viewHolder);
} else {
viewHolder = (FruitLoopsViewHolder) convertView.getTag();
determinePresentationMode(convertView, viewHolder);
}
/** [Omitted some code setting many of the fields above to] */
final Button button = viewHolder.getButton();
final View finalConvertView = convertView;
button.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
button.setEnabled(false);
button.setVisibility(View.INVISIBLE);
finalConvertView.setAlpha(0.5f);
finalConvertView.setBackgroundColor(Color.GRAY);
}
});
/**
* As using a final viewHolder to set the value inside the onClick() method of the
* button will cause the images not to update anymore, investigating the alpha value
* of the convertView seemed the only easy way to get there. Also, as it is a float,
* I want to leave a bit of extra padding in the test.
*/
if(finalConvertView.getAlpha() < 0.9)
{
viewHolder.setConsumed(true);
}
return convertView;
}
private void determinePresentationMode(View convertView, FruitLoopsViewHolder viewHolder)
{
if(viewHolder.wasConsumed())
{
convertView.setAlpha(0.5f);
convertView.setBackgroundColor(Color.GRAY);
}
}
}
最佳答案
我相信如果wasConsumed()
为false,则需要将alpha重置为初始状态。
就像是:
private void determinePresentationMode(View convertView, FruitLoopsViewHolder viewHolder)
{
if(viewHolder.wasConsumed())
{
convertView.setAlpha(0.5f);
convertView.setBackgroundColor(Color.GRAY);
} else {
convertView.setAlpha(1f); // A value higher of 0.9
convertView.setBackgroundColor(Color.WHITE); // or whatever color
}
}
您的问题是您正在重复使用相同的视图,但是您将拥有相同的Alpha。
我将状态信息保存在
ViewHolder
中,而不是根据convertView#alpha
确定状态