2017-08-22 9 views
0

現在、スクロール表示の子であるリニアレイアウト内に、相対レイアウト、線形レイアウト、またはネストされた線形レイアウトを追加できるようにするアプリを開発中です。スクロールは、相対レイアウトまたは線形レイアウトが追加されたときにはうまくいくように見えますが、ネストされた線形レイアウトが追加されると、スクロールが問題になります。基本的には、ドラッグしたビューが画面の上端/下端のx量内に来ると、スクロールビューをスクロールさせたいと思う。何かアドバイス?以下は、私のコードの一部は、XMLファイルのスクロール属性を実装するために、常にベストプラクティスをリニアレイアウトのドラッグ&ドロップスクロール

protected class myDragEventListener implements View.OnDragListener { 
    // This is the method that the system calls when it dispatches a drag event to the listener. 
    public boolean onDrag(View v, DragEvent event) { 
     // Defines a variable to store the action type for the incoming event 
     final int action = event.getAction(); 
     Point touchPosition = getTouchPositionFromDragEvent(v, event); 
     Log.d("y", String.format("%s", String.format("%s", eventTouchYCoord))); 
     //View dragView = (View) event.getLocalState(); 
     // Handles each of the expected events 
     switch(action) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       return true; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       return true; 
      case DragEvent.ACTION_DRAG_LOCATION: 
       Log.d("point", touchPosition.toString()); 
       if (touchPosition.y > (absoluteBottom + mScrollDistance - 350)){ 
        Log.d("bottom", "greater"); 
        Log.d("actionbar", String.format("%s", actionBarHeight())); 
        homeScroll.scrollBy(0, 30); 
       } 
       if (touchPosition.y < (actionBarHeight + 350)){ 
        Log.d("top", "greater"); 
        homeScroll.scrollBy(0, -30); 
       } 
       break; 

getTouchPositionFromDragEvent方法

public static Point getTouchPositionFromDragEvent(View item, DragEvent event) { 
    Rect rItem = new Rect(); 
    item.getGlobalVisibleRect(rItem); 
    return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY())); 
} 

onScrollChangedListener方法

homeScroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { 
     @Override 
     public void onScrollChanged() { 
      mScrollDistance = homeScroll.getScrollY(); 
      int[] coords = new int[2]; 
      homeScroll.getLocationOnScreen(coords); 
      absoluteTop = coords[1]; 
      absoluteBottom = coords[1] + homeScroll.getHeight(); 
     } 
    }); 
+0

私はより良い質問は、どのように私はスクロールどんなに、y位置がabsoluteTopの間のどこかになることを意味し、ドラッグビューの絶対位置を取得するんかもしれないと思いますand absoluteBottom。現時点では、getTouchPositionFromDragEventはスクロールを考慮に入れてレイアウト全体のx、y座標を返します。 –

答えて

0

そのを支援することです。これは、それを横にスクロールしたい。またはパレットでは、垂直スクロールとスクロールスクロールのオプションを見つけることができます。

<ScrollView 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
    
 
    </ScrollView>

+0

私はすでにXMLに実装されているscrollviewを持っています。私はコンテナをスクロールしても問題ありません。私の問題は、私がビューをドラッグしてスクロールしようとしているときです。 –

関連する問題