简体   繁体   English

OnItemClickListener不起作用,但OnLongItemClickListener在自定义列表视图中工作

[英]OnItemClickListener doesn't work but OnLongItemClickListener works in Custom Listview

I have a custom listview with custom adapter. 我有自定义适配器的自定义listview I want to click on the items of listview and do something. 我想点击listview的项目并做一些事情。 The OnItemClickListener does not works. OnItemClickListener不起作用。 But I implemented OnLongItemClickListener and it works perfectly. 但我实现了OnLongItemClickListener ,它完美地运行。

MainActivity 主要活动

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 custom adapter Adapter 更新自定义适配器适配器

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;
    }
};

} }

custom listview item.xml 自定义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 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>

THis is because of your CheckBox. 这是因为你的CheckBox。 You can solve like this: 你可以这样解决:

Add an id to your Linearlayout. 在您的Linearlayout中添加ID。

<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"
android:id="@+id/main_layout">

And in your getView 在你的getView中

LinearLayout main_layout = (LinearLayout)view.findViewById(R.id.main_layout));

main_layout.setOnClickListener(new OnClickListener() {

                            public void onClick(View v) {
                                listview .performItemClick(view,
                                        listview.getPositionForView(view),
                                        listview.getPositionForView(view));

                            }
                        });

Edit: Use following code for long click 编辑:使用以下代码进行长按

main_layout.setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View arg0) {
                                listview.performLongClick();
                                return true;
                            }
                        });

到您的文本视图和复选框

android:focusable="false"

You can give android:onClick attribute in xml : 你可以在xml中给android:onClick属性:

<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:onClick="onCheckboxClicked" />

This will work when you check or uncheck Checkbox. 当您选中或取消选中复选框时,这将起作用。

Reason why onClick do not work, but onLongClick works : onClick的原因不起作用,但onLongClick工作原理:

You have applied onListItemClickListener over your list view row so when you were clicking on checkbox, the event was consumed by this listener 您已在列表视图行上应用onListItemClickListener ,因此当您单击复选框时,此侦听器将使用该事件

listview.setOnItemClickListener(new OnItemClickListener() {

Update : 更新:

in getView() : getView()

    // for list row
 view.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
            Log.d("onClick","row click");

        }
});   

    // for list row check box
 view.cbBuy.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
              Log.d("onClick","checkbox click");

        }
}); 

In MainActivity : 在MainActivity中

Remove this part : 删除此部分:

/*   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();
        }
    }); */

Whether checkbox has property android:focusable="true" or false this code will run This is working compiled and run code Thank you 复选框是否具有属性android:focusable="true"false这个代码将运行这是工作编译并运行代码谢谢

@Amsheer your code works. @Amsheer您的代码有效。 But for that you had to change the custom adapter and write the actions for onClick events in it. 但为此,您必须更改自定义适配器并为其中的onClick事件编写操作。 I wanted to write the onClick events in the ManiActivity. 我想在ManiActivity中编写onClick事件。 I found a workaround. 我找到了一个解决方法。 I removed this from custom adapter 我从自定义适配器中删除了它

 view.setOnLongClickListener(new OnLongClickListener() {        
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return false;
        }
    });
view.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub  
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM