2014-02-17 84 views
9

我想把一個滾動視圖在另一個滾動視圖,我試着在這個網站上找到的答案,但它似乎仍然不完全工作。下面是XML:Android的滾動視圖在另一個滾動視圖不滾動

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:focusableInTouchMode="false" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textViewDirectoryDetailName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Name" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="@color/red" /> 

     <LinearLayout 
      android:id="@+id/linearLayoutDirectoryDetailContainImage" 
      android:layout_width="300dp" 
      android:layout_height="200dp" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:background="@drawable/general_image" 
      android:gravity="center" 
      android:orientation="vertical" > 
     </LinearLayout> 

     <ScrollView 
      android:id="@+id/scrollView2" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:focusableInTouchMode="false" > 

      <TextView 
       android:id="@+id/textViewDirectoryDescription" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:autoLink="phone" 
       android:text="098 123 45 678" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 
     </ScrollView> 

這裏是Java代碼:

parentScrollView = (ScrollView) findViewById(R.id.scrollView1); 
    childScrollView = (ScrollView) findViewById(R.id.scrollView2); 

    parentScrollView.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View p_v, MotionEvent p_event) { 
      childScrollView.getParent().requestDisallowInterceptTouchEvent(
        false); 
      // We will have to follow above for all scrollable contents 
      return false; 
     } 
    }); 
    childScrollView.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View p_v, MotionEvent p_event) { 
      // this will disallow the touch request for parent scroll on 
      // touch of child view 
      p_v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 
    // We will have to follow above for all child scrollable contents 

什麼似乎發生是這樣的:當我在TextView的文字似乎並沒有讓我滾動它在所有。它只是滾動父母。但是,當我沒有文本,我觸摸子ScrollView它不會滾動父,所以我想它的工作。但是,當我有文字時,你有什麼想法可以解決它不起作用的問題嗎?

+2

*我試圖把一個滾動型另一個滾動型內* - 不這樣做,你怎麼指望滾動上班的時候你有兩個不同的滾動區域? – Luksprog

+0

這實際上是一個壞主意。 –

+0

刪除scrollView2。你爲什麼首先把它放在那裏,你想達到什麼目的? – 2Dee

回答

25

您應該使用NestedScrollView

<android.support.v4.widget.NestedScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 
      <!-- --> 
</android.support.v4.widget.NestedScrollView> 
+1

我有另一個項目,同樣的問題,這工作。謝謝! –

0

檢查this鏈接,顯示如何在同一活動中使用兩個具有滾動功能的小部件。在這個檢查下面的代碼。

​​
+0

這是文學上面我已經有的相同的代碼,除了日誌,但我我也會試試這個,只是爲了100%確定。 –

+0

它根本不工作。而日誌總是說「家長接觸」,從來沒有「兒童接觸」,所以我想我不能觸摸子滾動視圖。 –

+0

http://stackoverflow.com/a/28944297/1835650 – TeeTracker

3

在另一個滾動視圖內滾動視圖不是一個好的做法。但是,您使用的代碼在某些情況下可能會起作用。但是當你有多個元素時它可能會失敗。在這種情況下,它可能無法識別手勢。

但是,您可以嘗試這個答案,如果它有幫助ScrollView Inside ScrollView。當識別到孩子的觸摸時,它禁用父ScrollView的觸摸。如果有幫助,也請看post

+0

我已經嘗試過這些方法,但我總是在日誌中獲得父母接觸並從未接​​觸過兒童。 –

+0

對不起,我無法理解你在說什麼。你可以展示你已經嘗試過什麼,以及你面臨的問題。 –

+0

我的意思是大多數方法都是一樣的。我用例如這一個:[鏈接](http://grishma102.blogspot.in/2014/01/handle-scrolling-of-scrollable-controls.html),當我觸摸子滾動視圖它仍然說,我已經觸及父母,而且根本不起作用。 –

1

你不能同時採用兩種滾動型滾動型操作系統中得到混淆其中一個必須滾動所以你的活動掛,所以不要這樣

0

使用我幾乎可以肯定這是不可能的:

如何安卓手柄觸摸事件:

家長獲取事件並決定是否必須處理它,或者是否應該讓它處理給孩子;

所以,如果一個滾動視圖處理觸感,一個處理它,始終是家長一個

http://developer.android.com/training/gestures/viewgroup.html

個人而言,我不知道有沒有辦法來了解什麼是感動背後父滾動查看是否是另一個滾動視圖,並讓它處理運動

2

我使用RectF來測試你的手指是否位於你的child_view的範圍內。

我已經將它用於scrollview和viewpager(包含兩個頁面,包含recycleviews)之間的組合。

試試這些代碼,你會得到你想要的。

findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      View childV = findViewById(R.id.child_view); 
      if (childV != null) { 
       int[] l = new int[2]; 
       childV.getLocationOnScreen(l); 
       RectF rect = new RectF(l[0], l[1], l[0] + childV.getWidth(), l[1] + childV.getHeight()); 
       if(rect.contains( event.getX(), event.getY())) { 
        childV.getParent() 
          .requestDisallowInterceptTouchEvent(false); 
        childV.onTouchEvent(event); 
        return true; 
       } 
      } 
      childV.getParent() 
        .requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 
2

老問題,雖然, 我張貼了我工作,我理解這個問題怎麼我已經通過它沒了,我只是刪除了滾動的TextView的XML格式,而實現在Java滾動功能爲TextView的碼。因此, XML是:

<TextView 
     android:id="@+id/textViewDirectoryDetailName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Name" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/red" /> 

    <LinearLayout 
     android:id="@+id/linearLayoutDirectoryDetailContainImage" 
     android:layout_width="300dp" 
     android:layout_height="200dp" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:background="@drawable/general_image" 
     android:gravity="center" 
     android:orientation="vertical" > 
    </LinearLayout> 
      <TextView 
      android:id="@+id/textViewDirectoryDescription" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:autoLink="phone" 
      android:text="098 123 45 678" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

和Java代碼將

TextView extViewDD = 
    (TextView)findViewById(R.id.textViewDirectoryDescription);  
    //for Scrolling of textView  
    textViewDD.setMovementMethod(new ScrollingMovementMethod()); 
//to stop parent linearlayout scrollview when touched on textview 
    textViewDD.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 
+0

很棒的回答。清潔和簡單。 – AhabLives

+0

謝謝!這是解決這個問題最簡單,最乾淨的解決方案。 –

0

使用這個自定義滾動型爲內滾動視圖:

public class TouchMeScrollView extends ScrollView { 
public TouchMeScrollView(Context context) { 
    super(context); 
} 

public TouchMeScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
} 

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