开发者

Gallery of TextViews, using styles etc

开发者 https://www.devze.com 2023-04-08 14:23 出处:网络
I want to use Gallery as a horizontal menu so I\'ve taken the code for the Gall开发者_开发技巧ery tutorial to use TextViews instead of ImageViews by using a String[] and changing the adapter\'s getVie

I want to use Gallery as a horizontal menu so I've taken the code for the Gall开发者_开发技巧ery tutorial to use TextViews 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...

Gallery of TextViews, using styles etc

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 TextViews 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消