2012-09-21 25 views
5

Tôi có một ScrollView dọc (thực sự, một ScrollView tùy chỉnh, mã bên dưới) trong một ViewFlipper. ViewFlipper hoạt động tốt trừ khi tôi vuốt ngang qua ScrollView để lật đến chế độ xem trước đó/tiếp theo. Bản thân ScrollView hoạt động chính xác.Không thể lấy đường viền ngang trên một ScrollView trong một ViewFlipper để bong bóng lên

Đây là thiết kế. Hộp màu xanh lá cây là ScrollView, nên được dọc.

enter image description here

Đây là scrollview:

public class SCScrollView extends ScrollView { 

    private float xDistance, yDistance, lastX, lastY; 

    GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector()); 
    OnTouchListener gestureListener; 

    public SCScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     gestureListener = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      }  
     }; 

    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       xDistance = yDistance = 0f; 
       lastX = ev.getX(); 
       lastY = ev.getY(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       final float curX = ev.getX(); 
       final float curY = ev.getY(); 
       xDistance += Math.abs(curX - lastX); 
       yDistance += Math.abs(curY - lastY); 
       lastX = curX; 
       lastY = curY; 
       if(xDistance > yDistance) 
        return false; 
     } 

     return false; 
     //return super.onInterceptTouchEvent(ev); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     super.onTouchEvent(event); 
     return gestureDetector.onTouchEvent(event); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev){ 
     gestureDetector.onTouchEvent(ev); 
     super.dispatchTouchEvent(ev); 
     return true; 
    } 

    /** GestureDetector used to swipe between classes */ 
    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float  velocityY) { 
      return false; 
     } 
    } 

} 

Tôi đã cố gắng làm cơ sở cho giải pháp theo lời khuyên ở đây HorizontalScrollView within ScrollView Touch Handling và đây Swipe/Fling tab-changing in conjunction with ScrollView? nhưng bất kể những gì tôi trở về từ các callbacks, tôi không thể có được các ViewFlipper để xem fling trên ScrollView và thẳng thắn, tôi đang bị lạc trong chuỗi liên lạc và người nghe cử chỉ.

Mọi suy nghĩ?

Cảm ơn

+0

Bạn đã cố gắng thêm một OnTouchListener đến scrollview từ ViewFlipper, có lẽ bạn có thể nhận cách này TouchEvents cần thiết? – Artjom

+0

Xin chào Artjom. Yeh, đó là những gì tôi đã cố gắng đầu tiên nhưng có vẻ như ScrollView đang tiêu thụ fling horizantal, mặc dù nó không cần phải. Đó là lý do tại sao tôi đang cố gắng ghi đè lên ScrollView vì vậy tôi có thể ngăn chặn nó được tiêu thụ và bong bóng nó lên đến ViewFlipper cha mẹ nhưng không tốt cho đến nay: ( – Simon

Trả lời

6

Như trong tài liệu nói

http://developer.android.com/reference/android/widget/ScrollView.html

scrollview chỉ hỗ trợ di chuyển dọc. Để cuộn ngang, sử dụng HorizontalScrollView.

Vì vậy, tôi cần hỏi tại sao bạn thực sự muốn sử dụng chế độ xem cuộn tùy chỉnh của mình?

Nếu bạn chỉ có ý định để có thể di chuyển, bạn có thể không cần phải sử dụng nó

chỉ RelativeLayout.scrollBy (x, y); là đủ

Bạn có thể nhìn vào liên kết này

Scrollview vertical and horizontal in android

EDIT

Ok, Vì vậy, bạn cần phải thực hiện các cử chỉ phát hiện các fling qua scrollview.

Bạn có thể làm cho nó trong hoạt động của bạn mà không làm tùy chỉnh scrollview cũng

đầu tiên, bạn cần phải

implements OnGestureListener, OnTouchListener, GestureDetector.OnDoubleTapListener 

và trong lớp học của bạn tạo gestureDetector

gd = new GestureDetector(this); 

và đối tượng scrollview của bạn, vừa được khai báo

sv.setOnTouchListener(this); 

sau đó, trong override onTouch phương pháp

@Override 
public boolean onTouch(View v, MotionEvent event) { 
     onTouchEvent(event); // throw to onTouchEvent 

    return false; 
} 

và trong phương pháp onTouchEvent override

@Override 
public boolean onTouchEvent(MotionEvent me) 
{ 
    return gd.onTouchEvent(me); // gd = gesturedetector 
} 

nay ghi đè phương pháp onFling bạn như thế này

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 

    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     // "Left Swipe" 
     vf.showPrevious(); // vf = ViewFlipper 

    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     // "Right Swipe" 
     vf.showNext(); 
    } 

    return false; 
} 

và đây là toàn bộ mã

ViewFlipperActivity lớp

import android.os.Bundle; 
import android.app.Activity; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.View.OnTouchListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.widget.HorizontalScrollView; 
import android.widget.ScrollView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ViewFlipper; 

public class ViewFlipperActivity extends Activity implements OnGestureListener, OnTouchListener, GestureDetector.OnDoubleTapListener 
{ 

    TextView tv; 
    GestureDetector gd; 
    ScrollView sv; 
    ViewFlipper vf; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     gd = new GestureDetector(this); 

     setContentView(R.layout.activity_viewflipper); 

     tv = (TextView)findViewById(R.id.textView1); 
     sv = (ScrollView)findViewById(R.id.scrollView1); 
     vf = (ViewFlipper)findViewById(R.id.viewFlipper1); 

     sv.setOnTouchListener(this); 

    } 

    @Override 
    public boolean onDoubleTap(MotionEvent arg0) { 
     tv.setText("double tap"); 
     return false; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent arg0) { 
     tv.setText("double tap event"); 
     return false; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent arg0) { 
     tv.setText("single tap confirm"); 
     return false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent me) 
    { 
     return gd.onTouchEvent(me); 
    } 

    @Override 
    public boolean onDown(MotionEvent arg0) { 
     tv.setText("down"); 
     return false; 
    } 

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 

     if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      tv.setText("Left Swipe"); 
      vf.showPrevious(); 

     } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      tv.setText("Right Swipe"); 
      vf.showNext(); 
     } 

     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent arg0) { 
     tv.setText("long press"); 

    } 

    @Override 
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     tv.setText("scroll"); 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent arg0) { 
     tv.setText("show press"); 
    } 


    @Override 
    public boolean onSingleTapUp(MotionEvent arg0) { 
     tv.setText("single tab up"); 
     return false; 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     onTouchEvent(event); 

     return false; 
    } 
} 

và activity_viewflipper.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="TextView" /> 

    <ViewFlipper 
     android:id="@+id/viewFlipper1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/textView1" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <ScrollView 
       android:id="@+id/scrollView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_above="@+id/textView3" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:layout_marginLeft="28dp" 
       android:layout_marginTop="63dp" 
       android:layout_toLeftOf="@+id/textView2" > 

       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:scaleType="matrix" 
        android:src="@drawable/thailandmap" /> 

      </ScrollView> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignTop="@+id/scrollView1" 
       android:layout_marginRight="30dp" 
       android:text="TextView" /> 

      <TextView 
       android:id="@+id/textView3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignRight="@+id/scrollView1" 
       android:layout_marginBottom="250dp" 
       android:layout_marginRight="29dp" 
       android:text="TextView" /> 

     </RelativeLayout> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="matrix" 
      android:src="@drawable/thwriting" /> 

    </ViewFlipper> 

</RelativeLayout> 
+0

Xin chào và cảm ơn phản ứng của bạn.Tôi không cố gắng để có được một ScrollView horizantal. Tôi đang cố gắng để có được ViewFlipper để nhận ra các horizantal swipe trên ScrollView mà nên đi đến trước hoặc xem tiếp theo.Tôi đã chỉnh sửa các câu hỏi để làm cho nó (hy vọng) rõ ràng hơn – Simon

+0

OK, nhìn vào câu trả lời chỉnh sửa của tôi. –

+0

Cảm ơn người đàn ông, tôi sẽ cố gắng vào ngày mai và quay lại ... – Simon

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