0

I want to create a custom view that extends LinearLayout and has children, but also uses a custom layout:

res/layout/control_accordion.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="header"
            type="java.lang.String" />
    </data>

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?android:selectableItemBackground"
            android:background="?android:background"
            android:onClick="@{v -> presenter.setExpanded(!presenter.expanded)}"
            android:orientation="horizontal">
            <TextView
                style="@style/Widget.EditText.FullWidth"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:clickable="false"
                android:focusable="false"
                android:text="@{ header }" />

            <ImageView
                android:id="@+id/control_recurrence_expander"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_gravity="end|center_vertical"
                android:layout_marginEnd="16dp"
                android:src="@drawable/ic_arrow_drop_down"/>
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:divider="?android:dividerHorizontal"
            android:orientation="vertical"
            android:showDividers="beginning|middle"
            android:id="@+id/control_accordion_content">


        </LinearLayout>
    </LinearLayout>
</layout>

I want to then be able to use this control inside of other layouts and give it children, like this:

<com.myapplication.Accordion>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test" />
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 2" />
    </com.myapplication.Accordion>

And I would like to have the TextViews appear inside of control_accordion_content. How would I accomplish this?

laptou
  • 6,389
  • 2
  • 28
  • 59
  • If your custom `View` extends `LinearLayout`, then the outer `` in your layout XML isn't really necessary. Replace it with `` tags, and set the `orientation` in code, or on your custom `View`'s XML tags. Then, for adding the children to an internal `LinearLayout`, have a look at these: https://stackoverflow.com/a/41059842, https://stackoverflow.com/a/36947869. – Mike M. May 27 '18 at 06:17
  • 1
    @MikeM. This is perfect, thank you. – laptou May 27 '18 at 06:50

0 Answers0