Android Trick - select Gridview item and make the cell highlighted
As Title said, I am making a customized calendar view .
For date part I am using Gridview.
But after I set item background and using setSelection(int)
I could not highlighted the item I selected.
this is the drawable for cell view background.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_pressed="true">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_checked="true">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_selected="true">
<item android:drawable="@drawable/bg_white_round_corner" android:state_pressed="false">
</item></item></item></item></selector>
This is the gridview codes:
gvDates.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Object obj = arg0.getAdapter().getItem(arg2);
if(obj!=null && obj instanceof Calendar){
if(listener!=null){
listener.onDateSelected((Calendar) obj);
gvDates.requestFocusFromTouch();
gvDates.setSelection(arg2);
}
}
}
});
The key part is before setSelection(int) . have to call requestFocusFromTouch();
To be continue...for checking the reason...
For date part I am using Gridview.
But after I set item background and using setSelection(int)
I could not highlighted the item I selected.
this is the drawable for cell view background.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_pressed="true">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_checked="true">
<item android:drawable="@drawable/bg_purple_round_corner" android:state_selected="true">
<item android:drawable="@drawable/bg_white_round_corner" android:state_pressed="false">
</item></item></item></item></selector>
This is the gridview codes:
gvDates.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Object obj = arg0.getAdapter().getItem(arg2);
if(obj!=null && obj instanceof Calendar){
if(listener!=null){
listener.onDateSelected((Calendar) obj);
gvDates.requestFocusFromTouch();
gvDates.setSelection(arg2);
}
}
}
});
The key part is before setSelection(int) . have to call requestFocusFromTouch();
To be continue...for checking the reason...
Comments
Post a Comment