我是新来的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>
感谢您的解释!我有这个代码的另一个问题,也许你可以帮助我?我正在使用这个回收站视图来显示搜索结果。在同一个活动中有视图,一个编辑文本和一个搜索按钮,但出于某种原因,当键盘出现时,回收站只会在触摸edittext后出现。那么当我搜索和项目更改时,它只会在键盘隐藏后再次出现。任何线索可能会发生什么?我将发布活动的代码。 –