【问题标题】:Android ListView Pull to refresh and Swipe List item to reveal buttonsAndroid ListView 拉动刷新和滑动列表项以显示按钮
【发布时间】:2015-05-12 09:50:44
【问题描述】:

我正在开发 Android ListView。我通过 XListView 实现了拉动刷新,但现在我还想实现从左向右滑动以在此 ListView 上显示按钮列表项。我该怎么做?或者如何在 ListView 上添加 2 个相同的库。

我在 XML 中的 ListView 是。

<com.orderlyexpo.www.listview.refresh.XListView
        android:id="@+id/lvOrders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/gray_text"
        android:dividerHeight="@dimen/dp1x" /> 

【问题讨论】:

    标签: android android-listview pull-to-refresh swipe-gesture


    【解决方案1】:

    @覆盖 公共布尔 onInterceptTouchEvent(MotionEvent ev) { int action = MotionEventCompat.getActionMasked(ev); 最终浮点 x = ev.getX(); 最终浮点 y = ev.getY();

        if (isEnabled() && touchListener.isSwipeEnabled()) {
    
            if (touchState == TOUCH_STATE_SCROLLING_X) {
                return touchListener.onTouch(this, ev);
            }
    
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    checkInMoving(x, y);
                    return touchState == TOUCH_STATE_SCROLLING_Y;
                case MotionEvent.ACTION_DOWN:
                    super.onInterceptTouchEvent(ev);
                    touchListener.onTouch(this, ev);
                    touchState = TOUCH_STATE_REST;
                    lastMotionX = x;
                    lastMotionY = y;
                    return false;
                case MotionEvent.ACTION_CANCEL:
                    touchState = TOUCH_STATE_REST;
                    break;
                case MotionEvent.ACTION_UP:
                    touchListener.onTouch(this, ev);
                    return touchState == TOUCH_STATE_SCROLLING_Y;
                default:
                    break;
            }
        }
    
        return super.onInterceptTouchEvent(ev);
    }
    
    /**
     * Check if the user is moving the cell
     *
     * @param x Position X
     * @param y Position Y
     */
    private void checkInMoving(float x, float y) {
        final int xDiff = (int) Math.abs(x - lastMotionX);
        final int yDiff = (int) Math.abs(y - lastMotionY);
    
        final int touchSlop = this.touchSlop;
        boolean xMoved = xDiff > touchSlop;
        boolean yMoved = yDiff > touchSlop;
    
        if (xMoved) {
            touchState = TOUCH_STATE_SCROLLING_X;
            lastMotionX = x;
            lastMotionY = y;
        }
    
        if (yMoved) {
            touchState = TOUCH_STATE_SCROLLING_Y;
            lastMotionX = x;
            lastMotionY = y;
        }
    }
    

    Use this library

    【讨论】:

      【解决方案2】:

      不要使用任何库进行滑动,制作自己的视图,您可以使用拉来刷新相同的库。

      就这样吧。

      添加类名。

      SwipeDetector.java

      public class SwipeDetector implements View.OnTouchListener {
      
      public static enum Action {
          LR, // Left to Right
          RL, // Right to Left
          TB, // Top to bottom
          BT, // Bottom to Top
          None // when no action was detected
      }
      
      private static final String logTag = "SwipeDetector";
      private static final int MIN_DISTANCE = 100;
      private float downX, downY, upX, upY;
      private Action mSwipeDetected = Action.None;
      
      public boolean swipeDetected() {
          return mSwipeDetected != Action.None;
      }
      
      public Action getAction() {
          return mSwipeDetected;
      }
      
      public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              downX = event.getX();
              downY = event.getY();
              mSwipeDetected = Action.None;
              return false; // allow other events like Click to be processed
          }
          case MotionEvent.ACTION_MOVE: {
              upX = event.getX();
              upY = event.getY();
      
              float deltaX = downX - upX;
              float deltaY = downY - upY;
      
              // horizontal swipe detection
              if (Math.abs(deltaX) > MIN_DISTANCE) {
                  // left or right
                  if (deltaX < 0) {
                 //     Logger.show(Log.INFO,logTag, "Swipe Left to Right");
                      mSwipeDetected = Action.LR;
                      return true;
                  }
                  if (deltaX > 0) {
                //      Logger.show(Log.INFO,logTag, "Swipe Right to Left");
                      mSwipeDetected = Action.RL;
                      return true;
                  }
              } else 
      
                  // vertical swipe detection
                  if (Math.abs(deltaY) > MIN_DISTANCE) {
                      // top or down
                      if (deltaY < 0) {
                 //         Logger.show(Log.INFO,logTag, "Swipe Top to Bottom");
                          mSwipeDetected = Action.TB;
                          return false;
                      }
                      if (deltaY > 0) {
                 //         Logger.show(Log.INFO,logTag, "Swipe Bottom to Top");
                          mSwipeDetected = Action.BT;
                          return false;
                      }
                  } 
              return true;
          }
          }
          return false;
      }
      }
      

      然后从您的 ListView 或项目 onClickListner 方法中调用它。我从 baseadapter 的 ItemClick 调用它。

      convertView.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  if(swipeDetector.swipeDetected()) {
                  if(swipeDetector.getAction() == Action.LR) {
                      viewHolder.chatButton.setVisibility(View.VISIBLE);
                      //outterLayout
                      RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                      relativeParams.addRule(RelativeLayout.RIGHT_OF, viewHolder.chatButton.getId());
                      relativeParams.setMargins(20, 0, 0, 0);
                      viewHolder.outterLayout.setLayoutParams(relativeParams);
                      viewHolder.tvDeliver.setVisibility(View.GONE);
                      return;
                  } else {
                      viewHolder.chatButton.setVisibility(View.GONE);
                      RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                      relativeParams.setMargins(0, 0, 0, 0);
                      viewHolder.outterLayout.setLayoutParams(relativeParams);
                      return;
                  }
              } 
                  Toast.makeText(context, "Click", 2000).show();          
              }
          });
      

      【讨论】:

      • 我从 listview.setOnItemClickListener 调用了 swipedetector.java 但没有检测到我的滑动..
      【解决方案3】:

      您可以使用Navigation Drawer 或在 ViewPager 中使用 XListView - 在 Page(fragment) 上将有 XListView 和其他按钮。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        相关资源
        最近更新 更多