我的CustomersAdapter
有一个RecyclerView
课程,我试图在RecyclerView行上实现onclick事件。但不幸的是,它不适合我。
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 :(得分: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();
}
}
});
}
答案 1 :(得分:0)
你忘了设置setOnClickListener ..在onCreateViewHolder中添加setOnClickListener
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);
}
答案 2 :(得分:0)
您忘了setOnTouchListener();
列出项目布局。您可以为列表项的主要布局设置触摸侦听器。
答案 3 :(得分:0)
将listener设置为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) {
}
});
}
在onBindViewHolder方法中。
答案 4 :(得分: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
}
答案 5 :(得分:0)
ViewHolder
不是实施View
的{{1}}。在这种情况下,您可以像这样模仿它:
onClick()