2014-02-17 82 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); 
} 
}