2016-11-07 85 views
0

我有一個CustomersAdapter類爲我的RecyclerView,我嘗試在RecyclerView行上實現onclick事件。但不幸的是,它不適合我。Recycler View Onclick android不工作

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:customers_layout

<?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

感謝它的工作,但不知道爲什麼它不通過接口goona工作後很少選擇雁分鐘 –

+0

如果您使用的是接口,則必須將構造函數中的onClickListener()設置爲可變視圖。 – vidulaJ

0

你忘記設置setOnClickListener ..添加setOnClickListener在onCreateViewHolder

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

你忘了setOnTouchListener();你列表項的佈局。您可以爲您的列表項的主佈局設置觸摸監聽器。

0

將監聽器設置爲holder.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) { 

     } 
    }); 
} 

in 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

ViewHolder不是View實現onClick()。在這種情況下,您可以像這樣模擬:

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