2015-01-29 26 views

Trả lời

4

Tôi nghĩ rằng nó có thể được thực hiện bằng cách viết phức tạp ViewActions bằng cách thực hiện vuốt.

public static ViewAction swipeToTop() { 
    return new MySwipeAction(Swipe.FAST, 
     GeneralLocation.CENTER, 
     new CoordinatesProvider() { 
      @Override 
      public float[] calculateCoordinates(View view) { 
       float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view); 
       coordinates[1] = 0; 
       return coordinates; 
      } 
    }, Press.FINGER); 
} 


public static ViewAction swipeToBottom() { 
    return new MySwipeAction(Swipe.FAST, 
     GeneralLocation.CENTER, 
     new CoordinatesProvider() { 
      @Override 
      public float[] calculateCoordinates(View view) { 
       float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view); 
       coordinates[1] = view.getContext().getResources().getDisplayMetrics().heightPixels; 
       return coordinates; 
      } 
    }, Press.FINGER); 
} 

nơi MySwipeAction có thể là một cái gì đó như thế này:

public class MySwipeAction implements ViewAction { 
    public MySwipeAction(Swiper swiper, CoordinatesProvider startCoordProvide, CoordinatesProvider endCoordProvide, PrecisionDescriber precDesc) { 
      // store here in class variables to use in perform 
      ... 
    } 

    @Override public Matcher<View> getConstraints() {...} 

    @Override public String getDescription() {...} 

    @Override 
    public void perform(UiController uiController, View view) { 
     ... 
     float[] startCoord = startCoordProvide.calculateCoordinates(view); 
     float[] finalCoord = endCoordProvide.calculateCoordinates(view); 
     float[] precision = precDesc.describePrecision(); 

     Swiper.Status status = Swiper.Status.FAILURE; 

     // you could try this for several times until Swiper.Status is achieved or try count is reached 
     try { 
      status = m_swiper.sendSwipe(uiController, startCoord, finalCoord, precision); 
     } catch (RuntimeException re) { 
      ... 
     } 

     // ensures that the swipe has been run. 
     uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration()); 
    } 
} 

Tôi hy vọng điều này có thể giúp bạn.

+2

là quá nhiều nhận xét và dấu chấm ... nếu bạn có "đầy đủ" ví dụ về mã của mình, vui lòng hiển thị.Nhiều người không biết những gì họ có thể nhập vào của bạn '...' – Morozov

+0

Tôi đồng ý. Thật khó hiểu để loại trừ các định nghĩa và bài tập biến được sử dụng trong mã của bạn. Nó không có giá trị tiết kiệm một vài dòng nếu nó làm cho mã khó đọc hơn. – Project

9

Bạn đã thử một GeneralSwipeAction như thế?

private static ViewAction swipeFromTopToBottom() { 
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER, 
      GeneralLocation.BOTTOM_CENTER, Press.FINGER); 
} 

Có lẽ bạn phải cung cấp một thực hiện tùy chỉnh cho tham số thứ hai và/hoặc thứ ba, như Anna đã đề cập:

new CoordinatesProvider() { 
    @Override 
    public float[] calculateCoordinates(View view) { 
     float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view); 
     coordinates[1] = 0; 
     return coordinates; 
    } 
} 
0

@testsingh tôi nghĩ rằng không có phương pháp out-off-hộp làm "swipeTop" hoặc "swipeBottom" sau vòng lặp có lẽ cách dễ nhất (dumbest) mà làm việc cho tôi XDDD

// swipeUp 100 lần, ngược lại cho swipeDown

for(int i=0;i<=100;i++){ 
    onView(withText("label")).perform(swipeUp()); 
} 
0

Ví dụ, nếu bạn sử dụng recyclerView trong ứng dụng của bạn, bạn có thể sử dụng một cái gì đó như thế này:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp()) 

hoặc

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeDown()) 
-1

Tôi biết tôi là muộn để đảng nhưng sau khá một chút của finagling cuối cùng tôi đã tìm thấy một cái gì đó có thể swipe trên xuống dưới, trái sang phải, vv - mà không cần bất kỳ id tài nguyên.

Lý do tôi cần nó là cho chế độ xem tự động, nơi mọi thứ hoàn toàn mơ hồ. Với phương pháp bên dưới, tôi có thể cuộn tất cả các cách xuống dưới và thậm chí thay đổi độ trễ để cuộn xuống chỉ một trang.

static void swiper(int start, int end, int delay) { 
    long downTime = SystemClock.uptimeMillis(); 
    long eventTime = SystemClock.uptimeMillis(); 
    Instrumentation inst = getInstrumentation(); 

    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 500, start, 0); 
    inst.sendPointerSync(event); 
    eventTime = SystemClock.uptimeMillis() + delay; 
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 500, end, 0); 
    inst.sendPointerSync(event); 
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 500, end, 0); 
    inst.sendPointerSync(event); 
    SystemClock.sleep(2000); //The wait is important to scroll 
} 

Tôi không cần phải sang trái, v.v. vì vậy tôi đã mã hóa 500 trong đó (500 là trục x).

và kêu gọi chúng tôi đã làm nó tôi đã làm điều này -

// This swipes all the way to the bottom of the screen 
public static void swipeToBottom(){ 
    swiper(1000, 100, 0) 
} 

// This scrolls down one page at a time 
public static void scrollSlowlyDown(){ 
    swiper(775, 100, 100); 
} 

// This swipes to the top 
public static void swipeToTop(){ 
    swiper(100, 1000, 0); 
} 

// This scrolls up one page at a time 
public static void scrollSlowlyUp(){ 
    swiper(100, 775, 100); 
} 

Tôi hy vọng điều này sẽ giúp bất cứ ai tình cờ lên này.

+0

không chắc chắn lý do tại sao tôi đã downvoted - điều này hoạt động khá tốt – Nefariis

+0

tiếc là tại targetSdk 26, chạy trên Android 7 này ném một: java.lang.SecurityException: Tiêm vào một ứng dụng khác yêu cầu quyền INJECT_EVENTS, mà chỉ được cấp cho các ứng dụng hệ thống. – cV2

Các vấn đề liên quan