我在我的代码中遇到了一些奇怪的行为,您将在下面找到。我的每一行都有一个按钮,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);
}
}
}