在最后一个listview Item上没有调用OnFling

时间:2014-02-25 10:41:57

标签: android listview swipe gesture onfling

我已经为我的listview项目实现了一个手势检测器,其中检测到onfling手势,将显示两个按钮。前几个列表项工作正常。

然而,当我滚动到listview的底部时,onfling将不适用于listview的最后一项,因此不会显示两个按钮。 PS:我仍然可以获得最后一个列表项的位置。 我目前正在我的ListFragment类中实现它。

如何启用onFling以处理我的上一个列表项?谢谢你的帮助。

使用代码编辑

public class ExampleFragment extends ListFragment{
    protected GestureDetector gestureDetector;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector());
        getListView().setOnTouchListener(new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if (gestureDetector.onTouchEvent(event)) {
                  MotionEvent cancelEvent = MotionEvent.obtain(event);
                  cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                  v.onTouchEvent(cancelEvent);
                  return true;
              }
              return false;
          }
      });  
}

 class MyGestureDetector extends SimpleOnGestureListener {

      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            int pos = getListView().pointToPosition((int)e1.getX(), (int)e1.getY());

          try {
              if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                      return false;

              if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                  Toast.makeText(getActivity(), "Left Swipe", Toast.LENGTH_SHORT).show();
              }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(getActivity(), "Right Swipe", Toast.LENGTH_SHORT).show();
                    View v =  getListView().getChildAt(pos);

                     if (v != null) {
                        Button edit = (Button) v.findViewById(R.id.edit);
                        Button connect = (Button) v.findViewById(R.id.connect); 
                        if (edit.getVisibility() == View.GONE && connect.getVisibility() == View.GONE){
                            edit.setVisibility(View.VISIBLE);
                               connect.setVisibility(View.VISIBLE);
                           }
                    } 

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

    }

1 个答案:

答案 0 :(得分:0)

@pskink,谢谢你,你的评论确实给了我一些想法,适配器实际上并没有保持视图本身,listview保持视图并将它们传递给适配器。然后,适配器将重新创建视图。下面是经过编辑的运行源代码,希望它能帮助任何面临同样问题的人。

public class ExampleFragment extends ListFragment{
protected GestureDetector gestureDetector;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector());
    getListView().setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          if (gestureDetector.onTouchEvent(event)) {
              MotionEvent cancelEvent = MotionEvent.obtain(event);
              cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
              v.onTouchEvent(cancelEvent);
              return true;
          }
          return false;
      }
  });  
}

class MyGestureDetector extends SimpleOnGestureListener {

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int pos = getListView().pointToPosition((int)e1.getX(), (int)e1.getY());

      try {
          if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                  return false;

          if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
              Toast.makeText(getActivity(), "Left Swipe", Toast.LENGTH_SHORT).show();
          }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getActivity(), "Right Swipe", Toast.LENGTH_SHORT).show();
                final View v = getListView().getAdapter().getView(pos, null, getListView());
                 if (v != null) {
                    Button edit = (Button) v.findViewById(R.id.edit);
                    Button connect = (Button) v.findViewById(R.id.connect); 
                    if (edit.getVisibility() == View.GONE && connect.getVisibility() == View.GONE){
                        edit.setVisibility(View.VISIBLE);
                           connect.setVisibility(View.VISIBLE);
                       }
                } 

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}