2

I have a ScrollView that contains a complex LinearLayout with various elements, among which there are several TextView with maxHeight defined and a long text inside. I need to enable scrolling the text inside the TextView when touch-and-dragging inside it, and the normal scroll of the ScrollView when touch-and-drag outside of them.

Is there a proper and elegant way to do this - if at all possible - or do I have to get dirty and start overriding the onTouchEvent handlers on all elements involved?

UPD 1: Judging from this there is no elegant solution, and the UI will have to be rethinked to include only one layer of scrollable elements.

Community
  • 1
  • 1
Igor K.
  • 750
  • 9
  • 25

3 Answers3

6

So it's not pretty, but you can extend from TextView and block the scrollview from scrolling. It's not as nice unless your textviews are large enough. After using this solution we found that it is best if you have a "show more" label that the users can click to show the full thing. Anyway, extend from TextView and override the onTouchEvent method:

  public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      this.getParent().requestDisallowInterceptTouchEvent(true);
    }
    if (ev.getAction() == MotionEvent.ACTION_UP) {
      this.getParent().requestDisallowInterceptTouchEvent(false);
    }

    return super.onTouchEvent(ev);
  }
dmon
  • 30,048
  • 8
  • 87
  • 96
0

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

 <ScrollView   
    android:id="@+id/ScrollView01"  
  android:layout_height="150px"   
  android:layout_width="fill_parent">  

   <TextView   
   android:id="@+id/TEXT_VIEW"   
   android:layout_height="wrap_content"   
   android:layout_width="wrap_content"   
   android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />  
 </ScrollView>  
  • I know that. My `ScrollView` has a `LinearLayout` as child, and the `TextView`s I'm talking about go inside the `LinearLayout`. But I can't get them to have their own scrolling, the `ScrollView`'s scroll takes over. I thought there might be a property of some sort that causes the child's scroll handling to take precedence over the parent's one. – Igor K. Mar 01 '12 at 13:57
0

you can see the APIdemos for more information, there is a a view of lot of text's with their own stylized scrollview in the same windows. For more inforation you can visit:

http://developer.android.com/resources/samples/ApiDemos/index.html

Pedro Teran
  • 1,200
  • 3
  • 17
  • 43
  • The only thing I could find in the API demos is a `ScrollView` having `LinearLayout`s added dynamically. It has nothing (that I could see) related to `TextView` nested inside a `ScrollView`. – Igor K. Mar 01 '12 at 13:55