Question

I have a gridview that I'm using to create a calendar. I am having trouble keeping the initial selection highlighted when I click outside the calendar/gridview. The calendar/gridview occupies the upper half of the view and is in a fragment and a corresponding list will go below. My layout for the grid in the fragment is as follows:

  <GridView
     android:id="@+id/weekGrid"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:listSelector="@color/green"
     android:numColumns="7" >
  </GridView>

To highlight an initial cell, I set its position by using the following after setting the adapter:

  calGrid.requestFocusFromTouch();
  calGrid.setSelection(startPosition);

The initial selection is green as desired. However, as soon as I click away from gridview the green background disappears. This does not happen if I am to select another cell and then click away from the gridview. In this case the highlighting remains. I have tried setting a onFocusChange listener on the gridview. But this is not being triggered when clicking outside the gridview. I have also tried using a selector in the android:listSelector.

Thanks in advance for your help.

Was it helpful?

Solution

Try this:

Replace:

calGrid.requestFocusFromTouch();
calGrid.setSelection(startPosition);

with:

((ImageView)calGrid.getChildAt(startPosition)).setSelected(true);

I think it should solve your problem, if you aren't using an ImageView, change the cast to the kind of view you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top