我在下面找到的代码中遇到了一些奇怪的行为。我的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);
}
}
}
答案 0 :(得分:2)
如果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
答案 1 :(得分:0)
删除if (convertView == null)
条件,然后尝试使用getView()
@Override
public View getView(int position, View convertView, ViewGroup parentViewGroup)
{
FruitLoopsViewHolder viewHolder;
final FruitLoopsNode FruitLoopsNode = provider.get(position);
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);
/** [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;
}