How to Implement a Long Click Listener on a Listview

how to implement a long click listener on a listview

You have to set setOnItemLongClickListener() in the ListView:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub

Log.v("long clicked","pos: " + pos);

return true;
}
});

The XML for each item in the list (should you use a custom XML) must have android:longClickable="true" as well (or you can use the convenience method lv.setLongClickable(true);). This way you can have a list with only some items responding to longclick.

Hope this will help you.

Set long click listener for listview

Your question is very similar to this one, but it looks like it's not an exact duplicate.

What you've noticed is that the ListActivity class does not have a method override specifically for this case.

In order to add this functionality as a method override, your class should implement the AdapterView.OnItemLongClickListener interface, and then you can add the onItemLongClick() method override, which acts just as the onListItemClick() method override you already have, but responds to long clicks.

Just make sure that you follow instructions from this answer, you must use android:longClickable="true" in the layout xml, or call listview.setLongClickable(true);

Example:

public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {

ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listview = (ListView) findViewById(R.id.list);

listview.setLongClickable(true);

}

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

Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();

return true;
}

protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);

Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();

}

//....................

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

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

Android creating a popup dialog by long click on listview item

So I solved the problem.

  1. Using another java class called ViewListContents and changed the place where my popup_layout is, I moved it into the menu and set some menu tags and not constraint tags.

  2. I've change the R.layout to R.menu in my onItemLongClickListener

In this class I used it like this:

    listView = (ListView) findViewById(R.id.listView);

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
PopupMenu p = new PopupMenu(ViewListContents.this, v);
MenuInflater inflater = p.getMenuInflater();
inflater.inflate(R.menu.popup_layout, p.getMenu());
p.show();
return true;
}
});

How can I implement Long Click Listener for listview

Assuming you are using a ListActivity you should change the following line (from your first code block)...

ListView list = new ListView(Monday.this);

...to be...

ListView list = getListView();

You can then set the listener for it.

How to long-click on listview and get position? and delete from the sql

you should use onItemLongClick(AdapterView<?> parent, View view, int position, long id)

redefine the i with position and it will work

as I can see from your code, you redefined the i variable in function public void onClick(DialogInterface dialogInterface, int i)

then, use position index variable to access adapter data

Long click to begin selecting items in a ListView?

Simple : OnLongClickListener

Then you need to manually remember what is selected or not. You need to notify the list from the changes and do something in the getView method of you adapter.

It would be a good practice to use the Contextual ActionBar mode to interact with all item at once, see here.

How to select an ListView item after long click?

It's possible, but just barely... I actually don't know how such a simple thing can wind up so ridiculously complicated.

The key to the answer can be found here: Android: keep blue background after ListView selection

What this boils down to is to define an additional style that is used by the ListView and setting the choice mode to AbsListView.CHOICE_MODE_SINGLE (as explained in the linked answer).

This allows you programmatically toggle the selection using Listview.setItemChecked(). However, you need to keep track of the index of the touched item in the onItemLongClick callback yourself, because ListView.setSelection() won't do that (at least ListView.getSelectedItem() will always return -1 as far as I can see).

Code (for simplicity, my fragment implements all three OnItemClickListener, OnItemLongClickListener, and
ActionMode.Callback):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.listViewAdapter = new ListViewAdapter();
this.root = (ListView)inflater.inflate(R.layout.fragment_bookmarks, container, false);
this.root.setAdapter(this.listViewAdapter);
this.root.setOnItemClickListener(this);
this.root.setOnItemLongClickListener(this);
this.root.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
return this.root;
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if(this.cabMode != null)
return false;
this.selectedPosition = position;
this.root.setItemChecked(position, true);

this.root.setOnItemClickListener(null);
this.cabMode = getActivity().startActionMode(this);
return true;
}

And finally, if you want to get rid of the selection when the CAB is closed:

@Override
public void onDestroyActionMode(ActionMode mode) {
cabMode = null;
this.root.setItemChecked(this.selectedPosition, false);
this.selectedPosition = -1;
this.root.setOnItemClickListener(this);
}

Registering and unregistering the OnItemClickListener makes sure that while the CAB is active you won't accidentally trigger the action usually associated with the item (like opening a detail view).



Related Topics



Leave a reply



Submit