带按钮的android listview不工作
我创建了一个listview适配器,其中一行包含一个按钮。它在我单击按钮时起作用,但在我单击行时不起作用 这是我的主要活动带按钮的android listview不工作,android,Android,我创建了一个listview适配器,其中一行包含一个按钮。它在我单击按钮时起作用,但在我单击行时不起作用 这是我的主要活动 public class MainActivity extends AppCompatActivity { . . . ListView lvProducts = (ListView) findViewById(R.id.lvProducts); lvProducts.addHeaderView(getLayoutInflater().inflate(R.layout
public class MainActivity extends AppCompatActivity {
.
.
.
ListView lvProducts = (ListView) findViewById(R.id.lvProducts);
lvProducts.addHeaderView(getLayoutInflater().inflate(R.layout.product_list_header, lvProducts, false));
ProductAdapter productAdapter = new ProductAdapter(this);
productAdapter.updateProducts(Constant.PRODUCT_LIST);
lvProducts.setAdapter(productAdapter);
lvProducts.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Product product = Constant.PRODUCT_LIST.get(position - 1);
Intent intent = new Intent(MainActivity.this, ProductActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
Log.d(TAG, "View product: " + product.getName());
intent.putExtras(bundle);
startActivity(intent);
}
});
适配器的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200px"
android:layout_margin="5dp"
android:minHeight="50dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/tvProductName"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:gravity="center"
android:text=""/>
<ImageView android:id="@+id/ivProductImage"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:id="@+id/tvProductPrice"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.24"
android:gravity="center"
android:text=""/>
<Button
android:id="@+id/tvAddItem"
android:layout_width="69dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="A"
/>
</LinearLayout>
因为适配器布局包含“单击侦听器”视图。也就是说,在您的情况下,您使用的是按钮视图,它将阻止listview单击listner。因此,在适配器父布局中,您需要添加块子代,如下所示
android:descendantFocusability="blocksDescendants"
在自定义布局文件集中
android:focusable="false"
android:focusableInTouchMode="false"
您需要为按钮和父布局添加侦听器,如下所示:
public class ProductAdapter extends BaseAdapter implements View.OnClickListener {
bAdd.setOnClickListener(this);
layout.setOnClickListener(this);
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show();
Cart cart = CartHelper.getCart();
Log.d(TAG, "Adding product: " + product.getName());
cart.add(product, 1);
Intent intent = new Intent(context, ShoppingCartActivity.class);
context.startActivity(intent);
}
}
}
您能粘贴适配器的xml代码吗?可能重复您所说的“当我单击按钮时它工作,但当我单击行时它不工作”是什么意思?如果您的问题是按钮,并且它在单击按钮时起作用,我不知道问题是它是否启动了不同的活动…对于按钮,它可以工作…它将启动shoppingcart活动…另一个问题是当我在行内单击时…它不会启动ProductActivity我对焦点做了一些研究…我尝试了以下按钮的内部适配器xml
android:focusable=“false”
…它可以工作…感谢guysit不工作…我对聚焦性做了一些研究…我尝试了以下方法android:focusable=“false”
…它可以工作
public class ProductAdapter extends BaseAdapter implements View.OnClickListener {
bAdd.setOnClickListener(this);
layout.setOnClickListener(this);
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show();
Cart cart = CartHelper.getCart();
Log.d(TAG, "Adding product: " + product.getName());
cart.add(product, 1);
Intent intent = new Intent(context, ShoppingCartActivity.class);
context.startActivity(intent);
}
}
}