查看的背景不令人耳目一新

时间:2012-06-08 09:22:05

标签: android android-gridview android-adapter

我有一个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选择了最后一项,则无法正常工作。

我希望我能清楚地解释这个问题,我们将不胜感激。

0 个答案:

没有答案