2017-09-26 4 views
0

私はAndroid開発の初心者で、の最初の投稿です。私は、各カードにオープンそれぞれのレイアウトをクリックしたときになるようにと、私はカードビューのクリックイベントを追加する方法ということをお願いしたいカードビューでonClickイベントを追加するには

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView    xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="10dp" 
android:id="@+id/card_view" 
xmlns="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="false" 
    android:orientation="vertical" 
    android:padding="12dp"> 

    <ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:elevation="12dp" 
     android:src="@drawable/profile" 
     tools:ignore="ContentDescription,UnusedAttribute" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Arbaz" 
     android:textSize="20sp" 
     tools:ignore="HardcodedText" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Arbaz" 
     android:textSize="18sp" 
     tools:ignore="HardcodedText" /> 


</LinearLayout> 

+0

Javaファイルも追加します。上記の答えは完全なコードではありません –

答えて

1

ユーザーがカードをクリックしたときに何をトリガしたい場合OnClickListenerをカードに配置する必要があります。

CardView card_view = (CardView) findViewById(R.id.card_view); // creating a CardView and assigning a value. 

card_view.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // do whatever you want to do on click (to launch any fragment or activity you need to put intent here.) 
     } 
    }); 
関連する問題