I want to use Gallery
as a horizontal menu so I've taken the code for the Gall开发者_开发技巧ery tutorial to use TextView
s instead of ImageViews
by using a String[]
and changing the adapter's getView((...)
method as follows...
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(mContext);
tv.setText(mMenuItems[position]);
tv.setLayoutParams(new Gallery.LayoutParams(150, 100));
return tv;
}
The problem I have now is as shown in the image...
As you can see, what should be the 'selected' TextView
is 'greyed' (although I can select it) and the other items are 'highlighted and I'd like the reverse. So the question is what is Gallery
doing to cause this and how do I override it?
UI stuff is a weak point of mine (I'm learning gradually honest) but I think I need to define styles as explained here...Definining styles and I understand what's being explained but how do I get the Gallery
to automatically apply the styles I define? Do I have to apply the styles the TextView
s I return from the adapter's getView(...)
method or can I apply them 'globally' to the Gallery
somehow?
You can leverage getSelectedItemPosition()
to update the view accordingly but you'll need a reference to the Gallery view (mGallery
below):
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(mContext);
tv.setText(mMenuItems[position]);
tv.setLayoutParams(new Gallery.LayoutParams(150, 100));
if (position == mGallery.getSelectedItemPosition()) {
// set tv properties to make it look selected
} else {
// set tv properties to make it look unselected
}
return tv;
}
精彩评论