ListView ItemClick ItemLong Click Issue android - android

I have a list view in which I have implemented both onItemClick and onItemLongClick.
The list view row is custom where there is checkbox which is selected when ItemClick happens.
Now WHen User does a long click, I am still getting a ItemClick which selects the checkbox and this looks odd as the user is trying to do something else.
How to fix this

Set an OnItemLongClickListener for your list view.
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
/*Make sure to return true so that the event will be consumed here, and not
propogated to the onListItemClick listener.*/
return true;
}
});
See similar answer here.

Related

android studio: how to update data of a list View made using custom Adapter

I have a list view made using custom Adapter & contains 2 text view in each row,
I want to update data of rows , when I long pressed on row.
Just update your model object according to the need and call notifyDataSetChanged() inOnLongClickListener or OnItemLongClickListener of ListView. List data will update.
Go to your custom adapter, and set an OnLongClickListener on the view you return in getView(). In the OnLongClickListener update the array or list you are using and when finished, call notifyDataSetChanged() on the ListView.
Try this:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
adapter.notifyDataSetChanged();
return true;
}
});

Android ListView setOnItemLongClickListener works but setOnItemClickListener does not

I wish to have a behaviour x when I click an item and a behaviour y when I push and hold the same item. I seem to be able to do one or the other, but not both.
So I have this Listview lv.
In onCreate I give it
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v(TAG, "click position: " + position);
}
});
And when I click any item in my listview I get the log.
Say I remove the setOnItemClickListener. And now I add:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
Log.v(TAG, "long click position: " + position);
return true;
}
});
When I do a long click (press and hold for 3s) I get the log. Yay.
But, if I try to use both together, the setOnItemLongClickListener eclipses the setOnItemClickListener. The onItemClick never gets called and only when I do a long click do I get the line logged.
The xml holding the items in the ListView has both:
android:longClickable="true"
android:clickable="true"
What fundamental concept about what I am trying to do did I get wrong?
If all else fails you can use onTouch on the listview and use 2 threads to handle long touches and short touches. It becomes alot more of a pain, but this is how i built a dragNdrop listview and swipe to delete listview. I hope that gives you a start in a way to solve this.
Ironically, from the xml containing my ListView items I removed the following:
android:longClickable="true"
android:clickable="true"
And now both listeners work as I intended them to!

ListView onClickListeners

I am trying to make long and normal onItemClickListener for my ListView. This code works but when I call the Long-click and removing my finger after the longClickListener was triggered onItemClickListener triggers too. What I'm doing wrong?
listView.setOnItemClickListener(new SubjectOnItemClick(listAdapter, getSherlockActivity()));
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new ColorPickerDialog(
getSherlockActivity(),
listAdapter,
position
);
return false;
}
});
Simpy,return true instead of return false in your onItemLongClick.
Returning true means telling Android you already got what you want, and need nothing more. Hence, the code will stop. (not triggering onClick)

How to disable all elements (also the ones not currently visible in the screen) in a listview?

I have a ListView with a set of elements. When I click one of the I would like to disable all the others. If I click again on the selected item all the other items are enabled again.
I tried the different proposed solution without success. I hope someone can help me.
This is my code:
//action to take when a presentation is selected
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
if(position!=selectedPresentation){
for(int i=0;i<parent.getCount();i++){
if(i!=position)//disable all the items except the select one
parent.getChildAt(i).setEnabled(false);
}
selectedPresentation=position;
}else if(selectedPresentation==position){
//enable again all the items
for(int i=0;i<parent.getCount();i++){
parent.getChildAt(i).setEnabled(true);
}
selectedPresentation=-1;
}
Where selectedPresentation is a global variable storing the selected item. If no item is selected its value is -1.
Thank you for your help!
Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return false for a given item in your the ones you want to disable.
In your custom ArrayAdapter overide isEnabled method as following
#Override
public boolean isEnabled(int position) {
return false;
}
other option
Don't implement onItemclickListener. This will not give you any update of item click. Only register onClick listener to views.
You may take another object of the List(Arraylist) which is populating elements in listview then on click copy the corresponding position data to new arraylist you made and then notifyDataSet and when you click again you populate listview with original list so it will apear again....Just do this trick it might work for you
parent.getChildeAt() only work for visible Item.
you should change something in adapter.
if make custom adapter then you can do some thing like #ṁᾶƔƏň ツ answer, but if you use default adapter you can change adapter's arraylist that before you pass it to adapter.
put boolean in it (in arraylist) and in on item click true/false it for all item.
I wrote this solution and seems it works fine!
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
Toast.makeText(context, "onClick called",Toast.LENGTH_SHORT).show(); // this will flash up twice
if(position!=selectedPresentation){
selectedPresentation=position;
for(int i=0;i<adapter.getCount();i++){
if(i!=position)//disable all the items except the select one
adapter.isEnabled(i);
}
}else if(selectedPresentation==position){
//enable again all the items
selectedPresentation=-1;
for(int i=0;i<adapter.getCount();i++){
adapter.isEnabled(i);
}
}
And in my adapter I wrote:
public boolean isEnabled(int position) {
if(selectedPresentation==-1 || selectedPresentation==position)
return true;
return false;
Now my concern is how to show the items in the listView as disabled

How to highlight multiple items in a list view?

Hello guys I want to highlight multiple items in a list view.
So I tried SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but it didn't help
I am using a custom adapter and extending BaseAdapter
I am using ListView and not AbsListView
I don't want to use CAB because it doesn't go well with the design of my app
I don't want to use the getView method of the adapter as well.
I don't want to use checkboxes either, I guess I will use a boolean for each item and pass it to getviews if I don't get a solution here, but that doesn't seem too elegant and neat. I believe there is a proper built-in way of doing it without using getview() of the adapter
I tried:
android:drawSelectorOnTop="false"
android:listSelector="#android:color/darker_gray"
in the xml but it is highlighting only one of the items and as soon as I click on another item, it highlights it instead...
So is there any proper way of doing it?
Here is how my app looks:
You can make the same logic as CAB but avoid using CAB.
Your list item should have the FrameLayout at root like
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/activatedBackgroundIndicator">
....
Set onItemClickListener to change choice mode on long press
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
// if already in multi choice - do nothing
return false;
}
mInMultiChoiceMode = true;
// set checked selected item and enter multi selection mode
final AbsListView list = (AbsListView) arg0;
list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
list.setItemChecked(arg2, true);
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
//exit multi choice mode if number of selected items is 0
if (((AbsListView) arg0).getCheckedItemCount() == 0) {
((AbsListView) arg0).setChoiceMode(AbsListView.CHOICE_MODE_NONE);
mInMultiChoiceMode = false;
}
} else {
// do whatever you should as in normal non-multi item click
System.out.println("CLICK");
}
}
});
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
It should be enough, but you have to use getView, to differentiate the selected and unselected state.
You can use isItemChecked() method to determine weather the item is selected or not, so you don't have to introduce a boolean variable for each items.
Edit:
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListView list = (ListView) parent;
if(list.isItemChecked(position)){
//...
}
else{
//...
}
use
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then manually do it in the adapter.

Categories

Resources