StackOverflowError when inflating a custom view that contains its own views

I have a fragment where I inflate "fragment_board.xml":

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.app.BoardView"
        android:id="@+id/view_board"/>
</FrameLayout>

      

As you can see the fragment panel contains a custom "BoardView" from where I want to load the following "view_board.xml":

<com.app.BoardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:fadingEdge="none"
    android:requiresFadingEdge="none"
    android:overScrollMode="always"
    android:id="@+id/board_scrollview_vertical">

    <android.widget.HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fadingEdge="none"
        android:requiresFadingEdge="none"
        android:overScrollMode="always"
        android:id="@+id/board_scrollview_horizontal"
        android:foregroundGravity="left">

    </android.widget.HorizontalScrollView>

</com.app.BoardView>

      

My custom view has two scrolling views (I use it for panning) and I want to be able to reuse it in other layouts. BoardView extends the (vertical) scrolling appearance in this way:

public class BoardView extends ScrollView

      

When I use it offline it inflates fine and I can findViewById () view the scrolls in the inflated layout. But when I use it in a layout tree (like a fragment) I run into problems.

In the snippet, I inflate the layout tree like this:

View view = inflater.inflate(R.layout.fragment_board, container, false)

      

From a snippet, I can findViewById () the outer (vertical) scroll view, but findViewById () returns null for the inner (horizontal) scroll.

In BoardView I inflate like this:

View view = inflate(getContext(), R.layout.view_board, this);

      

As I said, I can find both kinds of scrolling fine when they are pumped up on their own, but when they are pumped up as part of a fragment, I get a StackOverflowError.

It is clear why: first I inflate the fragment and then inflate the same XML in the view again, which causes another kind of inflation, etc. I am getting a circular link here. Problem I can't figure out how I can add an inner (horizontal) scroll view to an already existing layout. I think I need to somehow merge or inflate the layouts manually, but I can't figure out how.

Here are some links (which didn't work in my case):

Can anyone suggest what I should do. If possible, I would like the BoardView to act as a generic component that I can connect to the layout tree where needed.

Since Android [per one of the links above] does not officially support composite components, would it be better to drop XML layouts for internal views and add them to the code?

+3


source to share


1 answer


Please check if something like this fixes:

fragment_board.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <com.app.BoardView
        android:id="@+id/view_board"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

      



view_board.xml

 <merge xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:fadingEdge="none"
    android:requiresFadingEdge="none"
    android:overScrollMode="always"
    android:id="@+id/board_scrollview_vertical">

    <android.widget.HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fadingEdge="none"
        android:requiresFadingEdge="none"
        android:overScrollMode="always"
        android:id="@+id/board_scrollview_horizontal"
        android:foregroundGravity="left">

    </android.widget.HorizontalScrollView>

</merge>

      

More information on using MERGE and INCLUDE

+2


source







All Articles