我有一个 GridView 的自定义适配器,并且我实现了自己的选择处理,因为自定义视图吸收了网格的单击事件。一切正常,但现在我想添加通过代码选择项目的功能。我为此编写了一个函数并从活动中调用它,但是当用户选择另一个项目时,代码选定项目的背景(表示选择)没有得到更新!我尝试调用 postInvalidate,但没有任何效果。这是适配器的代码:
class PaintActionsAdapter extends BaseAdapter
{
private Context context;
private View[] listItemsViews;
private OnListItemClickListener itemClickListener;
private int[] actionsImagesResources;
private int lastSelectedPosition=-1;
private int pendingSelectedPosition=-1;
private class ActionButtonClickListener implements View.OnClickListener
{
private int position;
public ActionButtonClickListener(int position)
{ this.position=position; }
public void onClick(View view)
{
changeSelectedListItem(position);
if (itemClickListener!=null)
itemClickListener.onListItemClick(view,position);
}
}
public PaintActionsAdapter(Context context,OnListItemClickListener
itemClickListener)
{
if (context==null)
throw new IllegalArgumentException("The context must be non-null!");
this.context=context;
this.itemClickListener=itemClickListener;
TypedArray imagesResourcesNames=context.getResources().obtainTypedArray(
R.array.actions_images);
actionsImagesResources=new int[imagesResourcesNames.length()];
for (int counter=0;counter<actionsImagesResources.length;counter++)
{
actionsImagesResources[counter]=imagesResourcesNames.getResourceId(
counter,-1);
}
imagesResourcesNames.recycle();
listItemsViews=new View[actionsImagesResources.length];
}
public int getCount() { return actionsImagesResources.length; }
public long getItemId(int position)
{ return actionsImagesResources[position]; }
public Object getItem(int position)
{ return actionsImagesResources[position]; }
public View getView(int position,View convertView,ViewGroup parent)
{
ImageView actionImageView;
if (convertView==null)
{
actionImageView=new ImageView(context);
/*actionToggleButton=new ToggleButton(context);
actionToggleButton.setTextOn(null);
actionToggleButton.setTextOff(null);
actionToggleButton.setText(null);*/
AbsListView.LayoutParams layoutParams=new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.
WRAP_CONTENT);
actionImageView.setLayoutParams(layoutParams);
}
else actionImageView=(ImageView)convertView;
actionImageView.setImageResource(actionsImagesResources[position]);
actionImageView.setOnClickListener(new ActionButtonClickListener(position));
listItemsViews[position]=actionImageView;
if (pendingSelectedPosition==position)
{
changeSelectedListItem(position);
pendingSelectedPosition=-1;
}
return actionImageView;
}
public void setSelectedListItem(int position)
{
if ((position<0)||(position>actionsImagesResources.length))
throw new IndexOutOfBoundsException("The position of the list " +
"item to select must be between 0 and " + actionsImagesResources.
length + "! The value supplied was " + position);
if (listItemsViews[position]==null) pendingSelectedPosition=position;
else changeSelectedListItem(position);
}
private void changeSelectedListItem(int position)
{
listItemsViews[position].setBackgroundResource(R.drawable.
actions_list_selector);
if (lastSelectedPosition>-1)
{
listItemsViews[lastSelectedPosition].setBackgroundResource(0);
listItemsViews[lastSelectedPosition].postInvalidate();
}
lastSelectedPosition=position;
}
}
函数 setSelectedListItem 让活动更改选择,并调用 changeSelectedListItem,它使用 setBackgroundResource(0) 从最后选择的项目中删除选择指示。当用户通过单击选择项目时,这非常有效,但如果最后一个项目的选择是通过 setSelectedListItem 完成的,则不会。
我希望我清楚地解释了这个问题,任何帮助将不胜感激。