我有一个带有自定义适配器的自定义列表视图.我想单击列表视图的项目,然后执行某些 *** 作. OnItemClickListener不起作用.但是我实现了OnLongItemClickListener,它运行完美.
主要活动
public class MainActivity extends Activity {ArrayList<Product> products = new ArrayList<Product>();Adapter ListvIEwAdapter; //custom adapter objectListVIEw 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);}@OverrIDepublic int getCount() { return objects.size();}@OverrIDepublic Object getItem(int position) { return objects.get(position);}@OverrIDepublic long getItemID(int position) { return position;}@OverrIDepublic 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"><CheckBoxandroID:ID="@+ID/cbBox"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_gravity="center_vertical" ></CheckBox><linearLayoutandroID:ID="@+ID/linearLayout1"androID:layout_height="wrap_content" androID:orIEntation="vertical" ><TextVIEwandroID: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" ><ListVIEwandroID:ID="@+ID/lvMain"androID:layout_wIDth="match_parent"androID:layout_height="0dp"androID:longClickable="true"></ListVIEw>
解决方法:
到您的文本视图和复选框
androID:focusable="false"
总结 以上是内存溢出为你收集整理的java-OnItemClickListener不起作用,但OnLongItemClickListener在自定义Listview中起作用全部内容,希望文章能够帮你解决java-OnItemClickListener不起作用,但OnLongItemClickListener在自定义Listview中起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)