2016-12-13 7 views
0

私はRecyclerViewでImageButtonを使用しています。私はアプリケーションでClickListenerを処理したい、これはViewHolderのコードです。RecyclerViewのImageButton問題

FirebaseUIを使用しているため、アダプタはFirebaseRecyclerAdapterです。

RecyclerViewは正しく表示されますが、ImageButtonをクリックしても何もしません。

どうしたのですか?

これはviewHolderのコードである:

public class PicturesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

     Context context; 
     ImageButton imageButtonView; 
     TextView textIDView; 
     TextView latitudeView; 
     TextView longitudeView; 
     TextView dateView; 

     public PicturesHolder(View itemView) { 
      super(itemView); 
      this.context = itemView.getContext(); 
      imageButtonView = (ImageButton) itemView.findViewById(R.id.image); 
      textIDView = (TextView)itemView.findViewById(R.id.texto); 
      latitudeView=(TextView) itemView.findViewById(R.id.latitude); 
      longitudeView = (TextView) itemView.findViewById(R.id.longitude); 
      dateView = (TextView) itemView.findViewById(R.id.dateView); 

      itemView.setOnClickListener(this); 
      imageButtonView.setOnClickListener(this); 

     } 

     public void setImage(String url) { 
      Glide.with(context) 
        .load(url) 
        .into(imageButtonView); 
     } 

     public void setTextIDView(String textID) { 
      textIDView.setText(textID); 
     } 

     public void setLatitude(double latitude) 
     { 
      latitudeView.setText(String.valueOf(latitude)); 
     } 
    public void setLongitude(double longitude) 
    { 
     longitudeView.setText(String.valueOf(longitude)); 
    } 

    public void setDateView(String date) 
    { 
     dateView.setText(date); 
    } 

    @Override 
    public void onClick(View v) { 
      Toast.makeText(v.getContext(), "Item Pressed = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT); 
    } 
} 

Iはまた、このようなpopulateViewHolder()内部リスナーを使用しようとした:

public void onAttachRecyclerView() { 

    //Query lastHundred = mURLReference.limitToLast(100); 
    Query mQuery = mURLReference.orderByChild("date").limitToLast(100); 
    mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Pictures, PicturesHolder>(

      Pictures.class,R.layout.item_row,PicturesHolder.class, mQuery 

    ) { 
     @Override 
     protected void populateViewHolder(PicturesHolder viewHolder, Pictures model, final int position) { 
      viewHolder.setTextIDView(model.getPictureID()); 
      viewHolder.setImage(model.getDownloadURL()); 
      viewHolder.setLatitude(model.getLatitude()); 
      viewHolder.setLongitude(model.getLongitude()); 
      viewHolder.setDateView(model.getDate()); 

      viewHolder.imageButtonView.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View view){ 
        Toast.makeText(view.getContext(), "You clicked on " + position, Toast.LENGTH_SHORT); 
       } 
      }); 
     } 
    }; 
    mImagesRV.setAdapter(mRecyclerViewAdapter); 

} 

最後に、これはアイテムビューのXMLである:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="0.5"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/texto"/> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <ImageButton 
       android:src="@color/tw__composer_deep_gray" 
       android:layout_gravity="center" 
       android:padding="20dp" 
       android:layout_marginBottom="2dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="2dp" 
       android:layout_marginTop="2dp" 
       android:layout_height="300dp" 
       android:layout_width="300dp" 
       android:id="@+id/image" 
       android:layout_weight="0.39" /> 

     <TextView 
      android:text="Coordenadas:" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView" 
      android:textAppearance="@style/TextAppearance.AppCompat.Body2" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/latitude"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/longitude"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/espacioBlanco" 
      android:text=" "/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/dateView" 
      /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/orderNumber"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/darker_gray"/> 
    </android.support.v7.widget.CardView> 

</LinearLayout> 

おはようございます!

+0

でshow()を呼び出すのを忘れて働くと考えていますか? –

+0

@SauravGhimire done :) XMLが追加されました –

答えて

2

私はすべてがそれが必要として、あなたはちょうどあなたがアイテムビューのXMLを追加することができますトーストオブジェクト

Toast.makeText(..).show(); 
+0

ありがとうございます!私はその詳細を見ていない... –

関連する問題