2017-05-15 56 views
0

我有一個LinearLayout與嵌套RecyclerView顯示項目列表。當單擊RecyclerView(其中一個項目或背景白色區域)時,我想打開一個彈出窗口,但通常的setOnClickListener不起作用。Recyclerview不可點擊

當然,我可以在每個項目上放置點擊偵聽器,但它們之間的白色區域仍然不可點擊。

有沒有辦法讓整個RecyclerView區域可點擊?

編輯:我已經添加了一些示例代碼。我希望整個佈局可點擊打開彈出窗口,但前三個視圖的行爲正確,但RecyclerView沒有。

<LinearLayout 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/spacing_half" 
     android:background="@color/color_item_margin_divider"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/fragment_tags_title" 
     style="@style/ItemFragmentHeader"/> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/spacing_line" 
     android:background="@color/color_line_divider"/> 
    <RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/spacing_half"/> 
</LinearLayout> 
+0

抱歉,但是......怎麼能這樣幫助我的問題嗎? – Alessandro

+0

你應該發佈你的代碼。此外,請檢查您的父視圖,因爲它們可能會捕獲點擊事件。你可以通過向父母的XML添加'android:clickable =「true」'來禁用它,這可能會有所幫助。 – C0D3LIC1OU5

+0

請發佈一些代碼,我們會幫助您 –

回答

0

viewHolder中加上onClickListener。下面是我在那裏執行Listener

public class MyViewHolder extends RecyclerView.ViewHolder { 
     public ImageView shotThumbnail; 
     public MyViewHolder(View view) { 
        super(view); 
        shotThumbnail = (ImageView) view.findViewById(R.id.shotThumbnail); 
        shotThumbnail.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          //Put here stuff that you want the onclickListener should do 

         } 
        }); 

檢查這個鏈接可以幫助我的項目的一個片段: Detect click on RecyclerView outside of items

+0

正如我在問題中所說的,整個RecyclerView區域必須是可點擊的,不僅是單個項目,而且它們的邊緣也是可以點擊的。 – Alessandro

+0

分享它的圖像和可能標記您想要的區域使點擊? – Panda2109

+0

RecyclerView:padding +項目+它們的邊緣佔用的整個區域... – Alessandro