0

我想爲我的gridView實現OnItemClickListenerGridView OnItemClickListener不能使用cardView

我定義,像這樣在我的片段聽衆:

GridView gridview = (GridView)view.findViewById(R.id.gridview); 
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Log.d("imagedata","Position clicked: "+ position); 
     } 
    }); 

    List<ItemObject> allItems = ((MainActivity) getActivity()).getTinkeractivities(); //getJSON data 
    CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems, "home"); 
    gridview.setAdapter(customAdapter); 

我的每個項目在gridView佈局的樣子:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card_view" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:clickable="false" 
card_view:cardUseCompatPadding="true" 
card_view:cardCornerRadius="4dp" 
card_view:cardElevation="4dp"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="8dp"> 

    <ImageView 
     android:id="@+id/screen_shot" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:contentDescription="@string/hello_world" 
     android:layout_alignParentTop="true" 
     android:scaleType="centerCrop" 
     android:layout_centerHorizontal="true"/> 

    <TextView 
     android:id="@+id/activity_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/screen_shot" 
     android:layout_alignLeft="@+id/screen_shot" 
     android:text="@string/hello_world" 
     android:inputType="text" 
     android:textColor="#000" 
     android:layout_marginTop="8dp" 
     android:textSize="12sp"/> 

    <TextView 
     android:id="@+id/theme_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/activity_name" 
     android:layout_alignLeft="@+id/activity_name" 
     android:text="@string/hello_world" 
     android:textSize="12sp" 
     android:layout_marginTop="4dp"/> 

</RelativeLayout> 
</android.support.v7.widget.CardView> 

然而,點擊監聽,似乎並沒有被加工。我嘗試在卡片視圖的所有子視圖中將android:clickable設置爲false,並嘗試將其設置爲卡片視圖本身的false,但沒有成功。

我能夠在我的CustomAdapter中爲每個行視圖成功設置點擊監聽器,但我不想採取這種方法,因爲我想使用標準實現在我的gridview上使用custom action bar進行批量選擇。否則,我將不得不攪動longPress事件併爲我的行項目設置checked狀態,然後再努力實現適配器內的自定義操作欄。

要實現一個乾淨的實現,我想在我的片段中定義click偵聽器。我閱讀了幾篇關於Stackoverflow的博客文章和相關答案,所有這些都意味着採用適配器方法,並且沒有指定如何以正確的方式在活動或片段中工作。

我想知道在我的情況下是否不可能設置OnItemClickListener。如果不是,那我該如何去調試這個問題並解決問題呢?

+0

可能將'CardView'包裹在Rel中ativeLayout'。這應該工作。 –

+0

使用cardview的線性佈局 –

+0

@VoraAnkit,這不是你剛剛發佈的問題的重複。那裏的OP想要在他的gridView實現中爲子項目設置不同的點擊監聽器。我想爲我的網格視圖實現一個'OnItemClickListener'。問題是它沒有被調用,可能是因爲卡片視圖。我在佈局中的其他元素,即ImageView和TextView不應該干擾我猜測的行的可點擊性。 –

回答

2

你可以嘗試添加

android:descendantFocusability="blocksDescendants" 

要將網格項目


編輯:

不然,如果你有一個可以阻止鍵按鈕可以添加:

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

我應該在哪裏添加'android:descendantFocusability =「blocksDescendants」'。另外,我沒有任何按鈕,我只在我的佈局中有一個imageview和2個文本視圖。 –

+0

啊,將它添加到我的'RelativeLayout'裏面,我把'CardView'設置爲'android:focussable = false '在我的'CardView'上放置'android:descendantFocusability =「blocksDescendants」'放在我的'RelativeLayout'中,就像一個魅力!:) –

+0

是在父佈局中添加descendantFocusability :) –

相關問題