An Android library that lets you create an expandable layout in a simple and easy way in which you can use the default header and content OR pass your custom layout and just expand and collapse magic is all ready.
dependencies {
implementation 'com.widget:expandableLayout:3+'
}
<dependency>
<groupId>com.widget</groupId>
<artifactId>expandableLayout</artifactId>
<version>3+</version>
<type>pom</type>
</dependency>
<widget.com.expandablelayout.ExpandableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!--expand/collabse duration ,default 300-->
app:duration="400"
<!--default false-->
app:hideArrow="true"
app:arrow_icon="@drawable/arrow_down"
app:header_padding="16dp"
app:content_padding="10dp"
<!--default false-->
app:startExpanded="true"
<!--default 0-->
app:pinnedLineHeight="15dp"
<!--use your own custom layout-->
app:content_layout="@layout/custom_content"
app:header_layout="@layout/layout_expandable_header"
<!--Or use default layout which is a TextView-->
app:exp_title="Header default Text sample"
app:exp_content="Content Text Sample"
<!--fonts is the folder name in Assets-->
app:header_font="fonts/fontName.ttf"
app:content_font="fonts/fontName.ttf"'
app:header_color="@color/colorAccentDark"
app:content_color="@color/colorAccent"
app:header_padding="10dp"
app:content_padding="14dp"
app:header_text_style="italic"
app:content_style="bold"/>
Use refresh()
to remain the state(in case expanded while custom content includes RecyclerView whose data were updated then trigging refresh() will help the expandable sets the expand height well).
expandableLayout.setOnExpandedListener(new OnExpandedListener() {
@Override
public void onExpandChanged(View view, boolean isExpanded) {
//TODO handle onExpandChanged
}
});
ExpandableLayout expandableLayout = new ExpandableLayout(context)
.setHeaderTitle("Added By Java", Color.BLACK)
.setDefaultContent("Content xxx", Color.BLUE)
.setArrowDrawable(ContextCompat.getDrawable(this, R.drawable.arrow_down));
expandableLayout.setHeaderLayout(R.layout.custom_header);
.setContentLayout(R.layout.custom_content);
var expandableLayout = ExpandableLayout(context)
.setDefaultHeaderTitle("Added Through Kotlin")
.setDefaultContentTitle("Content xxx")
.setArrowDrawable(R.drawable.arrow_ic)
expandableLayout.setHeaderLayout(R.layout.custom_header)
.setContentLayout(R.layout.custom_content)
container.addView(expandableLayout)
expandable.getHeaderLayoutBinding();//returns ViewDataBinding which can be cast to your layout binding Impl class
expandable.getContentLayoutBinding();
expandable.getHeaderLayoutView();
expandable.getContentLayoutView();
In order to setup using in RecyclerView call ExpandableLayout.onAttachedToRecycler()
before binding items of the Adapter.
In order to enable one expanded per time in recyclerViewAdaper.onBind() call setRecyclerItem(linearLayoutManager,itemPosition)
with the RecyclerView's layoutManger(weather GridLayoutManager
or LinearLayoutManager
)
expandable.setRecyclerItem(linearLayoutManager, getAdapterPosition());
This project is licensed under the MIT License - see the LICENSE.md file for details