0

I am creating a custom view which extends ScrollView. In this view I need nested scrolling so, in my current approach, I'm actually extending android.support.v4.widget.NestedScrollView to support older versions because attribute android:nestedScrollingEnabled="true" for ScrollView is only available starting on API 21.

With the experiments I'm doing extending from one and the other, I would say that using NestedScrollView is less efficient. Then, I know that I can create two different custom views and let the client use one or the other with qualified layout resources directory.

But my question is: Is there a way to create a custom view which internally extends NestedScrollView if the device using it is running with an API < 21 BUT extends ScrollView with android:nestedScrollingEnabled="true" when running it on API >= 21 ? I think that would be better but don't if it is possible or how to do it.

nitrico
  • 543
  • 5
  • 9
  • You cannot dynamically change your superclass at runtime. – CommonsWare Jun 24 '16 at 00:19
  • @CommonsWare I know that, but maybe I can create them both and inflate one or the other depending on the current API. All of it using the same tag name for the layout would be great to avoid the client do the trick. – nitrico Jun 24 '16 at 00:22
  • 1
    "maybe I can create them both and inflate one or the other depending on the current API" -- use versioned layout resources: `layout/` for the `NestedScrollView` variant and `layout-v21/` for the `ScrollView` variant. "All of it using the same tag name for the layout would be great" -- that's not directly possible. The closest that you can do is use `` or `` to try to reduce the scope of duplication. – CommonsWare Jun 24 '16 at 00:27
  • Ok @CommonsWare that's what I thought but I hoped anyone figured out how to achieve it. I guess if it was possible the Android team itself would have taken this approach. Thank you so much :) – nitrico Jun 24 '16 at 00:33
  • 1
    as @CommonsWare said use two different layout files, but you can also use either `LayoutInflater.Factory` or `LayoutInflater.Factory2` and use one layout file with some custom tag, for example `` and create your view from `onCreateView` method – pskink Jun 24 '16 at 01:29

0 Answers0