2015-03-14 2 views
1

У меня есть 3 элемента в моем спискеView, проблема в том, что метод pointToPosition() возвращает 1 для первого элемента в списке и -1 для третьего элемента в списке. Сырые и сырые позиции правильны, поскольку они соответствуют позициям dX и dY, заданными инструментами dev dev. Поэтому я не могу работать, как получить правильную позицию курсора для элемента списка особенно последнее положение, как код возвращает -1, а не позиции 2. Я могу вставить дополнительный код, если требуетсяandroid list point to position

int pos = listView.pointToPosition((int) arg1.getRawX(), (int) arg1.getRawY()); 

Layout

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/x" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.buzz.ContactFragment" 
    tools:ignore="MergeRootFrame" 
    > 



<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <ListView 
     android:id="@+id/contact_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:divider="@null" 

     /> 
... 

Фрагмент

public class ContactFragment extends Fragment implements 
     LoaderManager.LoaderCallbacks<Cursor>, 
     GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, View.OnTouchListener { 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     gdc = new GestureDetectorCompat(getActivity().getApplicationContext(), this); 
     View rootView = inflater.inflate(
       R.layout.fragment_contact, container, false); 
     listView = (ListView) rootView.findViewById(R.id.contact_list); 
... 
} 

... 

@Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 

     int pos = listView.pointToPosition((int) arg1.getRawX(), (int) arg1.getRawY()); 
     Cursor cursor = (Cursor)listView.getItemAtPosition(pos); 
... 
} 
... 
+0

Почему бы вам не использовать onItemClickListener(); –

+0

Я хочу поддерживать салфетки, двойной кран и longpress, поэтому я использую gesturedetector – DeveloperDH

ответ

0

проблема была решена на основе предложений, найденных в Интернете, но извините не помню URL.

public int getTouchPosition(MotionEvent motionEvent){ 
    // Transient properties 
    int mDismissAnimationRefCount = 0; 
    float mDownX; 
    int mDownPosition=-1; 
    View mDownView=null; 

    // Find the child view that was touched (perform a hit test) 
    Rect rect = new Rect(); 
    int childCount = mListView.getChildCount(); 
    int[] listViewCoords = new int[2]; 
    mListView.getLocationOnScreen(listViewCoords); 
    int x = (int) motionEvent.getRawX() - listViewCoords[0]; 
    int y = (int) motionEvent.getRawY() - listViewCoords[1]; 
    View child; 
    for (int i = 0; i < childCount; i++) { 
      child = mListView.getChildAt(i); 
      child.getHitRect(rect); 
      if (rect.contains(x, y)) { 
        mDownView = child; 
        break; 
      } 
    } 

    if (mDownView != null) { 
      mDownX = motionEvent.getRawX(); 
      mDownPosition = mListView.getPositionForView(mDownView); 
    } 

    return mDownPosition; 

} 

Можно использовать положение, чтобы получить позицию курсора, используемую для заполнения вида

Cursor cursor = (Cursor)mListView.getItemAtPosition(mDownPosition);