2017-03-19 3 views
0

私はここで別の記事を読んでいますが、間違っていることは見つかりません。アイテムのリストを表示するRecyclerViewがあり、アイテムがクリックされたときにイベントを発生させたい。しかし、クリックしても何も起こらず、イベントは発生しません。ここ はfragementのレイアウトのコードである:そしてAndroid:RecyclerViewはonClickイベントを起動しません。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/recycler" 
    android:scrollbars="vertical" 
    android:clickable="true"/> 
</RelativeLayout> 

cardview(Iがクリック可能にすべてのコントローラを設定している):

<?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:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="wrap_content"> 
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="82dp" 
    android:layout_gravity="center" 
    android:layout_margin="5dp" 
    android:clickable="true" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardElevation="4dp"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:layout_marginLeft="10dp" 
      android:id="@+id/imageViewNotification" 
      android:layout_width="64dp" 
      android:layout_height="match_parent" 
      app:srcCompat="@drawable/stimmungsabgabe" 
      android:clickable="true"/> 
     <TextView 
      android:id="@+id/info_text" 
      android:layout_toRightOf="@id/imageViewNotification" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="Just want to test it" 
      android:textAlignment="center" 
      android:layout_marginLeft="5dp" 
      android:gravity="center" 
      android:clickable="true"/> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

そして表示fragementのコード、 recyclerview:

....  
@Override 
public void onStart() { 
    Notification n1 = new Notification("Trainingeintrag","Nun ist es soweit. Haben Sie heute trainert?", R.drawable.trainingseinheit); 
    Notification n2 = new Notification("Stimmungsabfrage", "Wie fühlen Sie sich in dem Moment?", R.drawable.stimmungsabgabe); 
    Notification n3 = new Notification("Fragebogen zur Aktivität", "Wie aktiv sind Sie?",R.drawable.aktivitaet_fragebogen); 
    Notification n4 = new Notification("Fitnessfragebogen", "Wie ist ihr aktueller Fitnessstand?",R.drawable.fitness_fragebogen); 


    notifications =Arrays.asList(n1,n2,n3,n4); 

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); 
    // setup notifications that appear only on specific occasions 
    final Notification nMotivationMessage = new Notification(
      "Schon gewusst?", 
      "Wissenswertes über Sport",R.drawable.trainingseinheit); 
    if(preferences.getBoolean("motivationMessage",false)) { 
     notifications.add(nMotivationMessage); 
     preferences.edit().remove("motivationMessage").commit(); 
    } 

    // fill the recycler 
    rv = (RecyclerView)view.findViewById(R.id.recycler); 
    LinearLayoutManager lm = new LinearLayoutManager(getContext()); 
    rv.setLayoutManager(lm); 
    // just create a list of tasks 
    rv.setAdapter(new NotificationAdapter(notifications, this)); 
} 

そしてアダプターのコード:

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> { 
List<Notification> notifications; 
TabFragment context; 
public class NotificationHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    CardView cv; 
    TextView tv; 
    ImageView imageView; 
    NotificationHolder(View itemView) { 
     super(itemView); 
     tv = (TextView) itemView.findViewById(R.id.info_text); 
     cv = (CardView) itemView.findViewById(R.id.card_view); 
     imageView = (ImageView) itemView.findViewById(R.id.imageViewNotification); 
    } 

    @Override 
    public void onClick(View view) { 
     Toast.makeText(ActivityMain.activityMain,"Clicked",Toast.LENGTH_SHORT).show(); 

     } 

    } 
} 
public void removeAt(int position) { 
    notifications.remove(position); 
    notifyItemRemoved(position); 
    notifyItemRangeChanged(position, notifications.size()); 
} 

public NotificationAdapter(List<Notification> notifications, TabFragment context){ 
    this.notifications = notifications; 
    this.context = context; 
} 

@Override 
public int getItemCount() { 
    return notifications.size(); 
} 

@Override 
public NotificationHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_notification, viewGroup, false); 
    NotificationHolder bvh = new NotificationHolder(v); 
    return bvh; 
} 

@Override 
public void onBindViewHolder(final NotificationHolder holder, final int position) { 
    NotificationHolder notificationHolder = (NotificationHolder) holder; 
    notificationHolder.tv.setText(notifications.get(position).getText()); 
    notificationHolder.imageView.setImageResource(notifications.get(position).getImage()); 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 
} 

機能onClickが呼び出されない理由が見つかりませんでした。

インサイドnotificationHolderコンストラクタはライン以下を追加

... ...愚かな間違いがあり

+1

'OnClickListener'として' NotificationHolder'を設定するのを忘れました。 –

+0

@MikeM。はい、それでした。ありがとう –

答えて

-1

...あなたはonclickのメソッドを実装しているが、あなたは、イベントをクリックし登録するのを忘れて任意の助けに感謝

登録するイベントをクリックします... itemView.setOnClickListener(this); これはうまくいくでしょう...

関連する問題