我是Android编程的新手,并尝试设置点击监听器来打开另一个活动。列表加载正常,但是当我单击项目时,没有任何反应。这是我的适配器:
public interface OnItemClickListener {
void onItemClick(Product product);
}
private List<Product> productList;
private double currentLatitude, currentLongitude;
private Context context;
private final OnItemClickListener listener;
public AdapterForProducts(List<Product> productList, OnItemClickListener listener, double currentLatitude, double longitude, Context context){
this.currentLatitude = currentLatitude;
this.currentLongitude = longitude;
this.productList = productList;
this.context = context;
this.listener = listener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
ViewHolderForProducts holder = new ViewHolderForProducts(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
ViewHolderForProducts holder = (ViewHolderForProducts) viewHolder;
Product product = productList.get(position);
String distanceText = "Distancia: " + round(distance(product.getLatitude(), product.getLongitude())) + " meters";
holder.bind(productList.get(position), listener, distanceText);
}
@Override
public int getItemCount() {
return productList.size();
}
viewHolder:
public ViewHolderForProducts(View view){
super(view);
cardView = (CardView) itemView.findViewById(R.id.cardView);
name = (TextView) view.findViewById(R.id.product_name);
value = (TextView) view.findViewById(R.id.product_value);
distance = (TextView) view.findViewById(R.id.product_distance);
}
public void bind(final Product product, final AdapterForProducts.OnItemClickListener listener,
String distanceText) {
name.setText(product.getName());
value.setText("Preço: " + product.getValue());
distance.setText(distanceText);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
}
我在这样的活动中称呼它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_and_show_product_list);
Bundle extras = getIntent().getExtras();
if (extras != null) {
latitude = extras.getDouble("latitude");
longitude = extras.getDouble("longitude");
}
query = "";
query_field = (EditText) findViewById(R.id.query_field);
searchButton = (Button) findViewById(R.id.searchProductButton);
requestQueue = Volley.newRequestQueue(getApplicationContext());
recyclerView = (RecyclerView) findViewById(R.id.recycler);
productList = new ArrayList<>();
fillProductListByName();
adapterForProducts = new AdapterForProducts(productList, new AdapterForProducts.OnItemClickListener() {
@Override
public void onItemClick(Product product) {
showProductOnMap(product);
}
}, latitude, longitude, getApplication());
recyclerView.setAdapter(adapterForProducts);
adapterForProducts.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
query = query_field.getText().toString();
productList.clear();
fillProductListByName();
adapterForProducts.notifyDataSetChanged();
}
});
}
这是列表项xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="@dimen/activity_vertical_margin"
app:cardElevation="@dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:clickable="true"
android:padding="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp" />
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="Product Name"
android:textSize="14sp" />
<TextView
android:id="@+id/product_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_toRightOf="@+id/product_name"
android:text="Price: " />
<TextView
android:id="@+id/product_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp"
android:layout_toEndOf="@+id/product_value"
android:layout_toRightOf="@+id/product_value"
android:text="Distance:" />
</RelativeLayout>
答案 0 :(得分:0)
我得到了它的工作。显然,如果列表项中有其他可点击对象,则此实现不起作用,如下所示:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:clickable="true"
android:padding="16dp">
我所做的就是擦除android:clickable =&#34; true&#34;来自RelativeLayout
答案 1 :(得分:0)
将attr android:clickable="true"
设置为父布局时,单击方法
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
只有在其子女没有设置clickable = true
或设置OnClickListener
时才会被调用。
实际上,您并不需要所有这些android:clickable="true"
。
当您设置android:clickable="true"
时,您要为此视图设置空ClickListener
。
单击事件将传递到布局层次结构中的最低视图,并且实现了ClickListener,即使是空体也是如此。这是您的错误的原因,因此您必须删除android:clickable="true"
。
删除android:clickable="true"
,删除空ClickListener
,现在布局层次结构中的最低视图是使用此ClickListener的CardView
:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(product);
}
});
所以,这是将被调用的ClickListener
。