0

我是新來的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

我明白了。從RelativeLayout的點擊=「真」:顯然,這種做法不如果該列表項內部的一些其他可點擊對象的工作,因爲是用的情況:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorAccent" 
    android:clickable="true" 
    android:padding="16dp"> 

我所做的只是抹去了android

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

Click事件被傳遞到佈局層次結構中的最低視圖,實現了ClickListener,即使是空主體。這是你的錯誤的原因,因此你必須刪除android:clickable="true"。 卸下android:clickable="true",刪除空ClickListener,現在在佈局層次結構中的最低視圖是CardView這個ClickListener:

itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       listener.onItemClick(product); 
      } 
     }); 

所以,這是ClickListener將被調用。

+0

感謝您的解釋!我有這個代碼的另一個問題,也許你可以幫助我?我正在使用這個回收站視圖來顯示搜索結果。在同一個活動中有視圖,一個編輯文本和一個搜索按鈕,但出於某種原因,當鍵盤出現時,回收站只會在觸摸edittext後出現。那麼當我搜索和項目更改時,它只會在鍵盤隱藏後再次出現。任何線索可能會發生什麼?我將發佈活動的代碼。 –