how to implement a long click listener and onclicklistener in single listview

允我心安 提交于 2019-12-10 17:01:36

问题


I am creating a app in android. In that i am using list view. now i want use both click event and long click event. if is possible can any help me to do.


回答1:


see this

Click & Long-Press Event Listeners in a ListActivity

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
        onListItemClick(v,pos,id);
    }
});

..

 lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
            return onLongListItemClick(v,pos,id);
        }
    });



回答2:


You just need to return true

list.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(final AdapterView<?> p, View v,final int po, long id) {

        // your code

    return true;
    }    
});

It basically tells the system that the Long press event has been handled (default is false), and no further events need to be handled (i.e. a single press, which inadvertently would happen in a long press event)




回答3:


You should use ListView.setOnItemClickListener for a simple click.

For the long click, you have a choice. If you want to perform a single action use ListView.setOnLongClickListener. If you want a context menu then register the list for a context menu, create the menu and the actions for it.

registerForContextMenu(ListView);

    @Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
       super.onCreateContextMenu(menu, v, menuInfo);
       // menu code here
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
     // menu habdling code here
     return super.onContextItemSelected(item);
}



回答4:


use ListView.setOnItemClickListener(listener) and ListView.setOnItemLongClickListener(listener) http://developer.android.com/guide/topics/ui/layout/listview.html




回答5:


Just use setOnItemClickListener() and setOnItemLongClickListener() on your listview.

listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
        }
    });
listView.setOnItemLongClickListener(new OnItemLongClickListener()
    {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3)
    {

    }
});



回答6:


use the following code.

    list.setOnItemClickListener(this);
    list.setOnItemLongClickListener(this);

Listener definitions will be :

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub


}

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    return false;
}



回答7:


itemToclick is the visible portion on which click u want some action

itemToClick.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v)      { 
      //do your logic on click 
     });
itemToClick.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
      // do your logic for long click and remember to return it 
        return true; }});


来源:https://stackoverflow.com/questions/11244491/how-to-implement-a-long-click-listener-and-onclicklistener-in-single-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!