Android 视图';背景不刷新

Android 视图';背景不刷新,android,android-gridview,android-adapter,Android,Android Gridview,Android Adapter,我有一个用于GridView的自定义适配器,我实现了自己的选择处理,因为自定义视图吸收了网格的点击事件。一切正常,但现在我想添加按代码选择项目的功能。我为此编写了一个函数,并从活动中调用它,但是当用户选择另一个项目时,代码所选项目(表示选择)的背景不会得到更新!我试着打电话给postInvalidate,但没有任何效果。以下是适配器的代码: class PaintActionsAdapter extends BaseAdapter { private Context context; priva

我有一个用于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;
}
}
类PaintActionsAdapter扩展BaseAdapter { 私人语境; 私有视图[]列表项视图; 仅专用项ClickListener项ClickListener; 私有int[]actionsImagesResources; private int lastSelectedPosition=-1; private int pendingSelectedPosition=-1; 私有类ActionButtonClicklListener实现View.OnClickListener { 私人职位; 公共操作按钮ClickListener(内部位置) {this.position=position;} 公共void onClick(视图) { changeSelectedListItem(位置); 如果(itemClickListener!=null) itemClickListener.onListItemClick(视图、位置); } } public PaintActionsAdapter(上下文,仅列出项ClickListener itemClickListener) { if(上下文==null) 抛出新的IllegalArgumentException(“上下文必须非null!”); this.context=context; this.itemClickListener=itemClickListener; TypedArray imagesResourcesNames=context.getResources().obtainTypedArray( R.array.actions(图像); actionsImagesResources=newint[imagesResourcesNames.length()]; 用于(整数计数器=0;计数器-1) { ListItemsView[lastSelectedPosition].setBackgroundResource(0); ListItemsView[lastSelectedPosition].postInvalidate(); } lastSelectedPosition=位置; } } 函数setSelectedListItem允许活动更改选择,并调用changeSelectedListItem,它使用setBackgroundResource(0)从上一个选择的项目中删除选择指示。当用户通过单击项目来选择项目时,这一点非常有效,但如果最后一个项目是通过setSelectedListItem选择的,则不适用

我希望我清楚地解释了这个问题,并将感谢任何帮助