【发布时间】:2016-10-14 08:03:26
【问题描述】:
我创建了一个列表视图适配器,其中一行包含一个按钮。当我点击按钮时它有效,但当我点击行时无效 这是我的主要活动
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);
}
});
这是适配器部分
public class ProductAdapter extends BaseAdapter {
bAdd.setOnClickListener(new View.OnClickListener() {
@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文件
<?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>
【问题讨论】:
-
能否粘贴您的适配器的xml代码
-
“当我单击按钮时它起作用但当我单击行时不起作用”是什么意思?如果您的问题是按钮并且单击按钮可以正常工作,我不知道问题是否是
-
它启动不同的活动...对于按钮它可以工作...它将启动购物车活动...另一个是当我在行内单击时...它不会启动产品活动
-
我对可聚焦性做了一些研究...我在适配器 xml 中尝试了以下按钮...
android:focusable="false"...它有效...谢谢大家
标签: android