gridview -image+text/keep selected item highlight

 public static class ViewHolder
    {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);
            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }
        view.txtViewTitle.setText(listCountry.get(position));
        view.imgViewFlag.setImageResource(listFlag.get(position));
        return convertView;
    }

Gridview have option to modify it’s selector (listSelector). When we click on item in Gridview, we saw blink blue and then dissapear after we release it.

To make it stay, all we need just declare it at “android:listSelector” in GridView XML.

<?xml version=”1.0″ encoding=”utf-8″?>
<GridView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/calendar_gridview”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:listSelector=”@color/caldroid_sky_blue”
android:adjustViewBounds=”true”
android:background=”@color/caldroid_lighter_gray”
android:gravity=”center_horizontal”
android:horizontalSpacing=”1dp”
android:numColumns=”7″
android:padding=”1dp”
android:stretchMode=”columnWidth”
android:verticalSpacing=”1dp” />

To make selector background not overriden / set in bottom side, use “setDrawSelectorOnTop(false)” :

gridView = (GridView) inflater.inflate(R.layout.date_grid_fragment, container, false);
gridView.setDrawSelectorOnTop(false); //work for gridview activity, but not the dialog.
焦点不见了是因为设置了background颜色,而selector的被background挡住了,设置这个属性即可                gridView.setDrawSelectorOnTop(true);
1.我自己用PS做了一张框图”frame”;
2.GridView.setSelector(frame)
3.GridView.setDrawSelectorOnTop(true)

 在adapter getView() 中设置 v.setSelected(true); 不起作用,而在点击的时候设置就有用?而一定要通过

gridview.setSelection(position);

这个是为啥?

我认为是当adapter初始化View之前已经设置select position ,通过onItemSelect 可以看到select 在 getView() 之前已经触发。

for (int i = 0; i < adatper.colorList.size(); i++) {
if (currentPenColor == adatper.colorList.get(i)) {
// gridViewColors.setDrawSelectorOnTop(true);
pos = i;
gridViewColors.post(new Runnable() {
@Override
public void run() {
gridViewColors.requestFocusFromTouch();
gridViewColors.setSelection(pos);
}
});
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: