i have small issues in event handling , i have List view custom adapter data , each row having date , title , price . when i click on row i need to display details page but when i long press on price , date or title i need to sort the list view. i need to use gesture for on Long Press. please refer below code what i have tried.
Custom Adapter View
public EventAdapterView(Context context, List<EventUtil> eventList) {
this.mContext = context;
this.mEventUtil = eventList;
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new DrawableManager();
}
#SuppressLint("DefaultLocale")
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
mView = convertView;
EventUtil eventUtil = mEventUtil.get(position);
mView = mLayoutInflater.inflate(R.layout.row_event_adapter, null);
TextView eventTitleView = (TextView) mView
.findViewById(R.id.list_view_event_title);
TextView eventDescView = (TextView) mView
.findViewById(R.id.list_view_event_location);
TextView eventDateView = (TextView) mView
.findViewById(R.id.list_view_event_price);
// final MyGestureDetector myGestureDetector= new MyGestureDetector();
// new ImageFeach().execute(mEventUtil.getEvent_Image_Url());
eventTitleView.setText(eventUtil.getEvent_Title());
// event title sorting
eventTitleView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil obje1,
EventUtil obje2) {
return obje1
.getEvent_Title()
.compareTo(
obje2.getEvent_Title());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event location sorting
eventDescView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_location()
.compareTo(
event2.getEvent_location());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event price sorting
eventDateView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
// if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_Price()
.compareTo(
event2.getEvent_Price());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event date sorting
ImageView dateImageView = (ImageView) mView
.findViewById(R.id.list_view_event_date);
dateImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event2
.getEvent_Date()
.compareTo(
event1.getEvent_Date());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
MyGestureDetector Class
public class MyGestureDetector extends SimpleOnGestureListener {
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
}
Help Me.. Thanks
I think, there's no need to use SimpleOnGestureListener.
In your Custom Adapter View:
First,
eventTitleView.setFocusable(false);
eventTitleView.setFocusableInTouchMode(false);
eventTitleView.setLongClickable(true);
eventDescView.setFocusable(false);
eventDescView.setFocusableInTouchMode(false);
eventDescView.setLongClickable(true);
eventDateView.setFocusable(false);
eventDateView.setFocusableInTouchMode(false);
eventDateView.setLongClickable(true);
Then,
just set setOnLongClickListener (View.OnLongClickListener l) on eventTitleView, eventDescView and eventDateView as,
eventTitleView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDescView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDateView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
Hope this helps you.
Why do you need to override onlongpress?
You know this function is available by default right?
just put the price , date or title on the same Layout and give each one OnLongPressListener with the action you want..:
EXAMPLE:
http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
I have done this thro onLongClickListener. It's easy using this interface . You need to do
Class YourClass implements onLongClickListener{
public boolean onLongClick(View arg0){
// Event generated when user have long pressed the screen
return false; // If you do not want the event to keep occuring again and again
}
ListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// YOUR SORTING CODE HERE
return false;
}
});
Related
I have a spinner and when an item is selected i want to recreate the activity. But when the activity is recreated it keeps constantly recreating because the new itemSelectedListener is triggered. I have fixed the bug but i am interested in why this is happening. Thank you in advance for any insights you offer.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
recreate();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You can avoid the first event with a flag in the OnItemSelectedListener.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
Boolean firstEventConsumed = false;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (firstEventConsumed) {
recreate();
} else {
firstEventConsumed = true;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
EDIT
I have found a solution but it is just a workaround. It is not a definitive solution.
It has a strange behaviour when you recreate the activity. When the activity is created for the first time, it doesn't call twice the onItemSelected, but when it is recreated it is called twice.
What I have done below is to control when the item is selected by the user (handling the touch event) and when it is done by the activity.
public class MainActivity extends AppCompatActivity {
private Boolean isUserAction = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the spinner
// create the adapter
spinner.setAdapter(spinnerAdapter);
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isUserAction = true;
return false;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (isUserAction) {
recreate();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
}
}
I have a recyclerview displaying a simple list of items. On long pressing a particular item, a piece of audio associated with this item (stored in the assets folder) is played. On releasing, the audio is paused, and can be returned to at the paused point by long pressing. To that end, I implemented an onTouchListener in the ViewHolder of the RecyclerViews Adapter and created an interface that I call in onBindViewHolder. It works but when I scroll through the items the audio plays automatically and loops endlessly. I can still long press an item and hear its audio playing on top of the automatically looping audio.
I'm not sure where exactly the problem is. I'm not sure if it relates to the way items are recycled in RecyclerView, given it's only something thats an issue when I scroll? I tried to add an onItemTouchListener in the the corresponding fragment and do the work over there but had the same issue.
Or maybe the problem is in how I've set up my onTouchListener and interface? I'm not sure I fully understand what should be happening in onInterceptTouchEvent.
Or does the issue maybe lie in how I've set up my mediaplayer in the onTouchEvent?
public class ClapsAdapter extends RecyclerView.Adapter<ClapsAdapter.ViewHolder> {
List<Clap> mItems;
private Context context;
ItemTouchListener itlistener;
GestureDetector mGestureDetector;
public interface ItemTouchListener {
boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e, List<Clap> list);
void onTouchEvent(List<Clap> list, View view, int position, MotionEvent me);
void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept);
}
#Override
public long getItemId(int position) {
return position;
}
public ClapsAdapter(Context mContext, List<Clap> myClap) {
super();
this.mItems = myClap;
this.context = mContext;
setHasStableIds(true);
mItems = new ArrayList<Clap>();
Clap mClaps = new Clap();
mClaps.setCName("Hearty Clap");
mClaps.setImageId(R.drawable.clapping1);
mClaps.setAudio("applause-01.mp3");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Business Clap");
mClaps.setImageId(R.drawable.clapping2);
mClaps.setAudio("fake_applause.mp3");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Green Clap");
mClaps.setImageId(R.drawable.clap3);
mClaps.setAudio("laugh_and_applause.mp3");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Slow Clap");
mClaps.setImageId(R.mipmap.slow_clap);
mClaps.setAudio("fake_applause.mp3");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Emoji Clap");
mClaps.setImageId(R.mipmap.clap_emoji);
mClaps.setAudio("light_applause.mp3");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Slow Clap");
mClaps.setImageId(R.mipmap.slow_clap);
mClaps.setAudio("laughter-1.wav");
mItems.add(mClaps);
mClaps = new Clap();
mClaps.setCName("Emoji Clap");
mClaps.setImageId(R.mipmap.clap_emoji);
mClaps.setAudio("laughter-2.mp3");
mItems.add(mClaps);
// this.itlistener = itl;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener{
public TextView title;
public ImageView imageView;
private ItemTouchListener touchListener;
final MediaPlayer mp = new MediaPlayer();
private Context mContext;
List<Clap> mItems;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.icon);
title = (TextView) itemView.findViewById(R.id.description);
itemView.setTag(itemView);
itemView.setClickable(true);
itemView.setOnTouchListener(this);
}
public void setTouchListener (ItemTouchListener itemTouchListener){
this.touchListener = itemTouchListener;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (touchListener != null) {
touchListener.onTouchEvent(mItems, v, getAdapterPosition(), event);
} return true;
}
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_clap, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Clap clap = mItems.get(position);
holder.title.setText(mItems.get(position).getCName());
holder.imageView.setImageResource(mItems.get(position).getImageId());
final MediaPlayer mp = new MediaPlayer();
holder.setTouchListener(new ItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e, List<Clap> list) {
View childView = rv.findChildViewUnder(e.getX(), e.getY());
if (childView != null && this != null && mGestureDetector.onTouchEvent(e)) {
this.onTouchEvent(list, rv,rv.getChildAdapterPosition(childView), e);
}
return true;
}
#Override
public void onTouchEvent(List<Clap> list, View view, int position, MotionEvent me) {
// Toast.makeText(context, "TOUCH ME!!!",
// Toast.LENGTH_SHORT).show();
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = view.getContext().getAssets().openFd(mItems.get(position).getAudio());
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setLooping(true);
mp.start();
}
break;
case MotionEvent.ACTION_UP: {
mp.pause();
Toast.makeText(context, mItems.get(position).getCName(),
Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
#Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
//return items.size();
return (null != mItems ? mItems.size() : 0);
}
public void insert(int position, Clap clap) {
mItems.add(position, clap);
notifyItemInserted(position);
}
public void remove(Clap clap) {
int position = mItems.indexOf(clap);
mItems.remove(position);
notifyItemRemoved(position);
}
Any help on the matter would be greatly appreciated. I feel like I've spent more time stuck on this than I should!
you should read about managing touch events in view group
http://developer.android.com/training/gestures/viewgroup.html
You can attach GestureDetector to your onTouchEvent() and then write your logic in onLongPress().
GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
//do something here
}
});
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
};
I want to capture the onClick event when the user selects a spinner value.
I've tried implementing OnClickListener and using the following code:
#Override
public void onClick(final View view) {
if (view == countrySpinner) {
Toast.makeText(this, "Override OK!", 3);
}
}
And binding with:
countrySpinner.setOnClickListener(this);
This compiles, but I get a RuntimeException advising me to use OnItemClickListener rather than OnClickListener for an AdapterView.
How can I capture that onClick event?
Instead of setting the spinner's OnClickListener,try setting OnTouchListener and OnKeyListener.
spinner.setOnTouchListener(spinnerOnTouch);
spinner.setOnKeyListener(spinnerOnKey);
and the listeners:
private View.OnTouchListener spinnerOnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Your code
}
return false;
}
};
private static View.OnKeyListener spinnerOnKey = new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//your code
return true;
} else {
return false;
}
}
};
Don't treat a spinner like a button, buttons have onClick events. Spinners have onItemSelected events.
You should be capturing the Spinner's onItemSelected event like this:
import android.widget.AdapterView;
Spinner productname_spinner =(Spinner) findViewById(R.id.your_spinner);
productname_spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
System.out.println(item.toString()); //prints the text in spinner item.
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
In Kotlin, similar to the following:
mySpinner.onItemSelectedListener = object: OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
// Do nothing
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val selection = parent?.getItemAtPosition(position)
viewModel.updateSelection(selection as String)
}
}
I've tried implementing OnItemSelectedListener to get Selected Spinner Value , using the following code:
String selectAreaNameString;
AreaSpinner.setOnItemSelectedListener(AreaSpinnerSelected = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
selectAreaNameString = item.toString();
});
To improve usability for blind users, I need a ListActivity with an OnItemSelected method to add stuff like vibration or sounds when a specific listitem is selected.
Therefore I have extended the ListActivity class and added the following methods:
protected void onListItemSelected(ListView parent, View v, int position, long id) {
}
private AdapterView.OnItemSelectedListener mOnSelectedListener = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
onListItemSelected((ListView)parent, v, position, id);
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
Now, when I create an instance of my new Class SelectableListActivity and override the onListItemSelected method, still nothing happens. Any Ideas? Many thanks in advance!
Problem solved. I had to override onContentChanged() and assign my listener to the ListView.
public class MyListActivity extends ListActivity {
private Vibrator vibrator;
private ListView mList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
setContentView(R.layout.station_list);
// do some other stuff
}
#Override
public void onContentChanged() {
super.onContentChanged();
mList = (ListView)findViewById(android.R.id.list);
mList.setOnItemSelectedListener(mOnSelectedListener);
}
private AdapterView.OnItemSelectedListener mOnSelectedListener = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View v, int position, long id) {
onListItemSelected((ListView)parent, v, position, id);
}
public void onNothingSelected(AdapterView parent) {
// nothing to do here...
}
};
protected void onListItemSelected(ListView parent, View v, int position, long id) {
vibrator.vibrate(100);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
vibrator.vibrate(100);
Cursor c = cursor;
c.moveToPosition(position);
Intent i = new Intent(this, SomeOtherActivity.class);
i.putExtra(StationsDbAdapter.KEY_ROWID, id);
startActivity(i);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
}
return true;
case KeyEvent.KEYCODE_MENU:
if (action == KeyEvent.ACTION_DOWN) {
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
}
This way I was able to create a ListView that is usable without the touchscreen and thus possible for e.g. blind people to interact with.
I want to add OnLongClickListener on my list view. Whenever the user long press the item in list some action should be performed, But my code does not catch this listener. Please let me know where I am going wrong. The similar code works for setOnItemClickListener very well.
Here is the code :
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
Log.d("in onLongClick");
String str=listView.getItemAtPosition(index).toString();
Log.d("long click : " +str);
return true;
}
});
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.
If your ListView row item refers to a separate XML file, be sure to add android:longClickable="true" to that layout file in addition to setting setOnItemLongClickListener() to your ListView.
or try this code:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
Toast.makeText(list.this,myList.getItemAtPosition(index).toString(), Toast.LENGTH_LONG).show();
return false;
}
});
I think this above code will work on LongClicking the listview, not the individual items.
why not use registerForContextMenu(listView). and then get the callback in OnCreateContextMenu.
For most use cases this will work same.
In xml add
<ListView android:longClickable="true">
In java file
lv.setLongClickable(true)
try this setOnItemLongClickListener()
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int pos, long l) {
//final String category = "Position at : "+pos;
final String category = ((TextView) view.findViewById(R.id.textView)).getText().toString();
Toast.makeText(getActivity(),""+category,Toast.LENGTH_LONG).show();
args = new Bundle();
args.putString("category", category);
return false;
}
});
this should work
ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "long clicked, "+"pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
also don't forget to in your xml android:longClickable="true" or if you have a custom view add this to your custom view class youCustomView.setLongClickable(true);
here is the output of the code above
I tried most of these answers and they were all failing for TextViews that had autolink enabled but also had to use long press in the same place!
I made a custom class that works.
public class TextViewLinkLongPressUrl extends TextView {
private boolean isLongClick = false;
public TextViewLinkLongPressUrl(Context context) {
super(context);
}
public TextViewLinkLongPressUrl(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewLinkLongPressUrl(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && isLongClick) {
isLongClick = false;
return false;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
isLongClick = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isLongClick = false;
}
return super.onTouchEvent(event);
}
#Override
public boolean performLongClick() {
isLongClick = true;
return super.performLongClick();
}
}
This worked for me for cardView and will work the same for listview inside adapter calss, within onBindViewHolder() function
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
If you want to do it in the adapter, you can simply do this:
itemView.setOnLongClickListener(new View.OnLongClickListener()
{
#Override
public boolean onLongClick(View v) {
Toast.makeText(mContext, "Long pressed on item", Toast.LENGTH_SHORT).show();
}
});
listView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return false;
}
});
Definitely does the trick.