我有一个带有自定义适配器的 GridView,当我从列表中选择一个项目时,选择器会显示一秒钟然后消失。我猜是自定义适配器导致了它,我尝试使用它和选择器,但无济于事。
这是我的适配器(基于“Hello, Views”中的 Grid View 示例):
package shira.android.paintdroid;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
class PaintActionsAdapter extends BaseAdapter
{
private Context context;
private int[] actionsImagesResources;
public PaintActionsAdapter(Context context)
{
if (context==null)
throw new IllegalArgumentException("The context must be non-null!");
this.context=context;
/*actionsImagesResources=context.getResources().getIntArray(R.array.
actions_images);*/
actionsImagesResources=new int[6];
for (int counter=0;counter<actionsImagesResources.length;counter++)
actionsImagesResources[counter]=R.drawable.pencil;
}
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);
AbsListView.LayoutParams layoutParams=new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.
FILL_PARENT);
actionImageView.setLayoutParams(layoutParams);
//actionImageView.setAlpha(0);
}
else actionImageView=(ImageView)convertView;
actionImageView.setImageResource(actionsImagesResources[position]);
return actionImageView;
}
}
这是布局:
<GridView android:id="@+id/paint_actions_grid" android:layout_width="0dip"
android:layout_height="fill_parent" android:numColumns="2"
android:layout_weight="0.15" android:drawSelectorOnTop="true"/> <!-- android:background="@color/ui_background_color"/> -->
<!-- android:listSelector="@drawable/actions_list_selector"/> -->
谢谢你的帮助。
这是 GridView 现在的样子的图像捕获(现在,所有项目都具有相同的图像)。我希望在所选图像周围绘制某种颜色的框架,但正如我所说,我得到一个橙色矩形,该矩形在一秒钟后消失。