Getting list position in scroll list

I know this question is being asked up to here . I tried as a gesture detector:

wholelistview.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }); 

which detects only swipe (cannot get the line position here).

similarly in getview:

 public View getView(int position, View convertView, ViewGroup parent) { //other stuff convertview.setOnTouchListener(same as above); } 

but can't detect wipes.

Any solution for this ??? I want only the position of the line on which the napkin is placed.

+4
source share
4 answers

hanry what errors do you get? I implemented it and it works correctly. But instead of preserving the position of the element, I save the view holder, which among others also has a model property. Remember that you need to check if the conversion is null. Take a look at the code:

 public View getView(int position, View convertView, ViewGroup parent) { View view = null; Model m = filter.subItems.get(position); if(m != null) { if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.rowlayout, null); ViewHolder viewHolder = new ViewHolder(); viewHolder.position = position; - here you can put your position. view.setOnTouchListener(this.listener); //assign whatever you like to the viewHolder - in most cases the model and inlated controls and then assign } else { view = convertView; } view.setTag(viewHolder); } 

and then

 public boolean onTouch(View v, MotionEvent event) { ViewHolder viewHolder = ((ViewHolder) v.getTag()); } 

hop that helps. ps: If you know nothing about the holders and tags to view, I come to there . And my approach was described in the link that Frankenstein gave.

+3
source

To get the line position for your view, get the view position passed onTouch for your list using this function

 wholelistview.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { int position = indexOfChild(View v); return true; } return false; } }); 

Gets the index of the child. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/ViewGroup.java#ViewGroup.indexOfChild%28android.view.View% 29th

+1
source

you can get getTag and setTag to know the presentation tag and use this to determine which line was skipped

 public View getView(int position, View convertView, ViewGroup parent) { //other stuff convertview.setTag(position); } 

and swipe the link

  public boolean onTouch(View v, MotionEvent event) { int pos = (Integer)v.getTag(); Log.i(TAG,pos+" is swiped"); } 

I have not implemented this ... so you have to test the error ... but for me it may be a good way to achieve what you want to do.

+1
source
 private int position = -1; @Override protected void onListItemClick(ListView l, View v, int index, long id) { position = index; } 

like this?

0
source

Source: https://habr.com/ru/post/1384665/


All Articles