Cilenco
Cilenco

Reputation: 7137

GridView selection does not work with custom layout

I have a GridView in my app with the following item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@android:drawable/list_selector_background" >

    <TextView
            android:id="@+id/notizenTitel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Titel"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

    <TextView
        android:id="@+id/notizenBeschreibung"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Beschreibung" />
</LinearLayout>

Now I want to select an item if the user clicks on it. I tried with this code:

gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
gv.setOnItemClickListener(this);

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    GridView gv = (GridView) getView().findViewById(R.id.notizenGrid);
    gv.setSelection(position);
    adapter.notifyDataSetChanged();
}

But now if I click on an item the background is changed to yellow shortly and dissapead than. So the selector is working while I click on an item but not for the selection. Do you have any ideas why?

Upvotes: 0

Views: 861

Answers (2)

subrahmanyam boyapati
subrahmanyam boyapati

Reputation: 2888

If you add andrid:clickable="true", your gridView will not preform click event, it will block all click events.

Upvotes: 0

Yogendra
Yogendra

Reputation: 5288

Add a property in linear layout

android:clickable = "true"

It will help.

Upvotes: 1

Related Questions