0

我有我的Recycler View一個View.OnClickListener,但每當我點擊一個視圖不會觸發onClick事件View.OnClickListener不工作

這裏是代碼:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    TextView name; 
    ImageView image; 
    Context context; 
    ArrayList<Categories_Model_Class> arrayList; 

    public ViewHolder(View view, Context context, ArrayList<Categories_Model_Class> arrayList) { 
     super(view); 
     name = view.findViewById(R.id.category_name); 
     image = view.findViewById(R.id.category_image); 
     this.context = context; 
     this.arrayList = arrayList; 
     view.setOnClickListener(this); 
     Log.i("Created", "ViewHolder"); 
    } 

    @Override 
    public void onClick(View view) { 
     //Pair<View, String> pair1 = Pair.create((View) this.image, this.image.getTransitionName()); 
     Intent intent = new Intent(context, FullWallpaperViewActivity.class); 
     //ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)context, pair1); 
     context.startActivity(intent); 
     Log.i("TOUCH", "TOUCH"); 
    } 
} 

有什麼建議?

  • 佈局的CardView模型類

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:focusable="true" 
    android:foreground="?attr/selectableItemBackground" 
    android:orientation="vertical"> 
    
    <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:layout_margin="4dp" 
    android:clickable="true" 
    android:focusable="true" 
    android:foreground="?attr/selectableItemBackground"> 
    
    
    <ImageView 
        android:id="@+id/category_image" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scaleType="centerCrop" 
        android:background="@color/colorBackground" /> 
    
    <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@drawable/bg_gradient" /> 
    
    <TextView 
        android:id="@+id/category_name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:fontFamily="sans-serif-smallcaps" 
        android:text="Category!" 
        android:textColor="@android:color/white" 
        android:textSize="24dp" /> 
    
    </FrameLayout> 
    </LinearLayout> 
    
+0

這可能有幫助。上次,我在這裏得到了解決方案。 https://stackoverflow.com/questions/24471109/recyclerview-onclick https://stackoverflow.com/questions/35970389/defining-a-recyclerviews-onclicklistener-in-an-activity –

+0

感謝您的努力,但仍然不工作:( –

+0

@YuganshTyagi檢查我的更新答案 –

回答

1

頂部佈局(<LinearLayout>,爲view在代碼中訪問)內的<FrameLayout>視圖自己處理單擊事件。

您需要從<FrameLayout>刪除android:clickable像這樣:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:layout_margin="4dp" 
    android:focusable="true" 
    android:foreground="?attr/selectableItemBackground"> 

如果不是,它會消耗掉點擊不會達到父< LinearLayout>

+0

添加布局,我在查看項目 –

+0

上使用適配器和回收站視圖感謝您的幫助,它的工作! :) –

+1

不客氣:) –

0
view.setClickable(true); 
view.setOnClickListener(this); 
+2

儘管這段代碼可能是解決方案,[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely- code-based-答案)確實有助於提高你的文章的質量,請記住你將來會回答讀者的問題,這些人可能不知道你的代碼建議 –

+0

感謝你的建議的原因,實際上,方法名稱是清楚的以便我沒有提及任何東西 –

+1

這很好,只是這個答案出現在低質量審查隊列。雖然我可以理解代碼的作用(確保ViewHolder構造函數中的'view'在分配'onClickListener'之前是可點擊的),那些剛開始使用Android的人可能不知道代碼的位置或者*重新使用'setClickable()' –

相關問題