2016-11-07 9 views
0

私はRecyclerViewのためにCustomersAdapterクラスを持っており、RecyclerView行のonclickイベントを実装しようとしています。しかし残念ながら私のために働いていない。Recycler View Onclick android not working

public class CustomersAdapter extends RecyclerView.Adapter<CustomersAdapter.MyViewHolder> { 

    private List<Customers> customerList; 
    Context ctx; 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.customers_layout, parent, false); 

     return new MyViewHolder(itemView); 

    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 

     Customers customer = customerList.get(position); 
     holder.customerName.setText(customer.getCustomerName()); 
    } 

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

    } 

    public CustomersAdapter(List<Customers> customerList,Context ctx) { 
     this.customerList = customerList; 
     this.ctx = ctx; 
    } 



    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     public TextView customerName; 

     public MyViewHolder(View view) { 
      super(view); 
      customerName = (TextView) view.findViewById(R.id.cust_name); 

     } 

     @Override 
     public void onClick(View v) { 
      int position = getAdapterPosition(); // gets item position 
      if (position != RecyclerView.NO_POSITION) { 
       Customers customer= customerList.get(position); 
       Toast.makeText(ctx, customer.getCustomerName(), Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:clickable="true" 
    > 

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      > 

      <ImageView 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:id="@+id/cust_img" 
       android:src="@drawable/person" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/cust_name" 
       android:textColor="#000" 
       android:text="HELLO" 

       android:layout_marginLeft="13dp" 
       android:layout_marginStart="13dp" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/cust_img" 
       android:layout_toEndOf="@+id/cust_img" /> 
     </RelativeLayout> 


    </android.support.v7.widget.CardView> 

</RelativeLayout> 
+0

エラーがありますか? – mcemilg

+0

アプリケーションは実行されていません。アプリをデバッグしようとしましたが、onclickメソッドには入っていませんでした。 –

答えて

1

代わりにこれを試してください。コンストラクタ内にonClick()を実装します。

public MyViewHolder(View view) { 
    super(view); 
    customerName = (TextView) view.findViewById(R.id.cust_name); 
    view.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int position = getAdapterPosition(); // gets item position 
      if (position != RecyclerView.NO_POSITION) { 
       Customers customer= customerList.get(position); 
       Toast.makeText(ctx, customer.getCustomerName(), Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 
+0

なぜなら、分 –

+0

インターフェイスを使用している場合は、コンストラクタ内のonClickListener()をビューに設定する必要があります。 – vidulaJ

0

customers_layoutあなたはonCreateViewHolder

あなたがリスト項目のレイアウトのために setOnTouchListener();に忘れてしまった
private final OnClickListener mOnClickListener = new MyOnClickListener(); 
@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.customers_layout, parent, false); 
    itemView.setOnClickListener(mOnClickListener); 
    return new MyViewHolder(itemView); 

} 
+0

MyViewHolderはonClickListenerインターフェイス –

+0

を実装していますが、customersAdapterはありません。onClickListener – sasikumar

0

でsetOnClickListenerを.. setOnClickListenerを設定追加するのを忘れ。リスト項目のメインレイアウトにタッチリスナーを設定することができます。

0

セットリスナーホルダー.itemView。

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 

    Customers customer = customerList.get(position); 
    holder.customerName.setText(customer.getCustomerName()); 
    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     } 
    }); 
} 

onBindViewHolderメソッド。

0

あなたのビューにonClickListenerを追加するのを忘れました。それはあなたのonclickが動作しない理由です。 クリックリスナーをビューに追加する必要があります。

public MyViewHolder(View view) { 
     super(view); 
     customerName = (TextView) view.findViewById(R.id.cust_name); 

     //bind the click listener with your view, like 
     customerName.setOnClickListerner(this); // now it will get called when you click this customerName view 
    } 
0

ViewHolderonClick()を実装Viewではありません。その場合は、次のようにエミュレートすることができます:

public MyViewHolder(View view) { 
    super(view); 
    customerName = (TextView) view.findViewById(R.id.cust_name); 
    //you forgot to call this 
    view.setOnClickListener(this); 
}