Reputation: 2948
I have a GridView with a custom adapter, and when I select an item from the list, the selector is shown for a second and disappears. I guess the custom adapter causes it, and I tried playing with it and the selector, but to no avail.
Here's my adapter (based on the Grid View sample in "Hello, Views"):
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;
}
}
And here's the layout:
<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"/> -->
Thanks for any help.
Here's an image capture of how the GridView looks like now (for now, all the items have the same image). I want a frame in some color to be drawn around the selected image, but I get an orange rectangle that disappears after a second, as I said.
Upvotes: 0
Views: 4762
Reputation: 16393
It is because in touch mode there is no such thing as selected. See the docs here.
You can get around this by setting your grid-cell layout to clickable android:clckickable="true"
and then creating a ColorStateList as described here.
The state you want to use for this is activated
. Use that state in your selector xml.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="@color/your_activated_color" />
<item android:color="@color/your_standard_color"/>
</selector>
Then set the selector xml as your background for the gridview cell layout. That should achieve the desired effect.
Upvotes: 5