I have implemented GalleryView. I want to display a border image on selected image from Gallery.
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));//, android.R.layout.simple_list_item_1, items));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int location,
long arg3) {
imageView.setImageResource(items.get(location));
final ImageView iv = (ImageView) adapter.getSelectedView();
iv.setBackgroundResource(R.drawable.large_button_sel_liner);
}
});
And my Adapter class
class ImageAdapter1 extends ArrayAdapter<Integer> {
private Context ctx;
private List<Integer> items;
public ImageAdapter1(Context context, int textViewResourceId,
List<Integer> objects) {
super(context, textViewResourceId, objects);
items = objects;
ctx = context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView iv = new ImageView(ctx);
iv.setImageResource(items.get(position));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
return iv;
}
}!
It is totally mashed up.
What has worked for me in the past is to create an Integer that tracks the click selection, and then apply the background in the getView() method if the position matches the click selection. So (roughly):
and then in the Adapter View:
You might have to do a bit more work to track the right selection (the click location and the position might not be the right variables to use), but otherwise something similar should work.
Inspired by Nishant Shan's reply, I elaborate my own solution:
First of all, create a border resource: common_galleryborder_shape.xml:
Then create a selector that use this shape: common_gallerycurrentitem_selector.xml
And, finally, add this code to your adapter class:
In addition, is also possible set Gallery.setSpacing(int) value to avoid overlapping images inside the gallery.
I have got the solution using Selector.
I have created galleryselector.xml
and set it in Adapter class in getView():