gpt4 book ai didi

java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用

转载 作者:可可西里 更新时间:2023-10-31 22:04:06 26 4
gpt4 key购买 nike

我有一个带有自定义适配器的自定义 listview。我想点击 listview 的项目并做一些事情。 OnItemClickListener 不起作用。但我实现了 OnLongItemClickListener,它运行得非常好。

主 Activity

public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
Adapter listviewAdapter; //custom adapter object
ListView listview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.lvMain);
listview.setLongClickable(true);
listviewAdapter = new Adapter(this, products);
listview.setAdapter(listviewAdapter);
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) { //this works
Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
return false;
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) { //does not work
Toast.makeText(getApplicationContext(), " pressed", Toast.LENGTH_SHORT).show();
}
});
}

UPDATE 自定义适配器适配器

public class Adapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
TextView itemname,itemprice;
Adapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
itemname= ((TextView) view.findViewById(R.id.tvDescr));
itemname.setText(p.name);
itemprice=((TextView) view.findViewById(R.id.tvPrice));
itemprice.setText(p.price + "");
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.selected);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
});
view.setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> selected = new ArrayList<Product>();
for (Product p : objects) {
if (p.selected)
selected.add(p);
}
return selected;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
getProduct((Integer) buttonView.getTag()).selected = isChecked;
}
};

自定义 ListView item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
</CheckBox>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:longClickable="true">
</ListView>

最佳答案

到您的 TextView 和复选框

android:focusable="false"

关于java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com