Android TabLayout with multiple lines text description - android

I need to show a multiple-line text description below my tabs icons (actually I need two lines but trying to describe it generally).
Using a one-word description will impair the readability and I do not want to use a scrollable tab layout because I need to show just 4 options.
In addition, using TextView as a custom view for the tab layout just feels wrong.
I've tried to do a custom layout for tab item and use it but for some reason words-break in the middle.
Is there any option to show the tab description but not as a single line

Solved with custom layout and OnTabSelectedListener.
My custom layout (tab_item.xml):
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/tab_item_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/some_icon_drawable" />
<TextView
android:id="#+id/tab_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:autoText="false"
android:breakStrategy="simple"
android:singleLine="false"
android:text="Two Lines Text View"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tab_item_icon" /></androidx.constraintlayout.widget.ConstraintLayout>
My setTabs and method:
private void setTabs(){
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
icons.add(R.drawable.some_drawable_1);
icons.add(R.drawable.some_drawable_2);
icons.add(R.drawable.some_drawable_3);
icons.add(R.drawable.some_drawable_4);
tabsNames.add(getString(R.string.some_tab_name_1));
tabsNames.add(getString(R.string.some_tab_name_2));
tabsNames.add(getString(R.string.some_tab_name_3));
tabsNames.add(getString(R.string.some_tab_name_4));
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> tab.setCustomView(R.layout.tab_item)
).attach();
for(int currTab = 0; currTab < tabsNames.size() ; currTab++){
tabTextView = (tabLayout.getTabAt(currTab).getCustomView().findViewById(R.id.tab_item_text));
tabImageView = (tabLayout.getTabAt(currTab).getCustomView().findViewById(R.id.tab_item_icon));
tabTextView.setText(tabsNames.get(currTab));
tabImageView.setImageDrawable(getResources().getDrawable(icons.get(currTab), null));
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tabTextView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_text));
tabImageView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_icon));
tabTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
tabImageView.setColorFilter(getResources().getColor(R.color.colorPrimary));
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tabTextView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_text));
tabImageView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_icon));
tabTextView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
tabImageView.setColorFilter(getResources().getColor(R.color.colorPrimaryDark));
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
tabTextView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_text));
tabImageView = (tabLayout.getTabAt(tab.getPosition()).getCustomView().findViewById(R.id.tab_item_icon));
tabTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
tabImageView.setColorFilter(getResources().getColor(R.color.colorPrimary));
}
});
tabLayout.getTabAt(0).select();
}

Related

How to InCrease or resize icon in tabitems in tablayout? in Android

I draw tablayout contains tabItems with icon
I want to resize(Increase) Icon size
How can I do???
first, I draw tablayout in xml and draw tabItems in tablayout
second, I make selector(xml) use for tabItems,
third, I make tab seletedListener in java class
4th, I linked selected event, and it works well
But, has a problem icons size are too small
this is result
enter image description here
It looks so small
this is xml
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvBestTitle"
app:tabIndicatorColor="#004098"
app:tabIndicatorFullWidth="true"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#004098"
app:tabTextAppearance="#style/TabLayoutTextStyle">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/selector_food_icon"
android:text="식품" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/selector_ship_icon"
android:text="조선" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/selector_steel_icon"
android:text="철강" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/selector_car_icon"
android:text="자동차" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="#drawable/selector_electronic_icon"
android:text="전자" />
</com.google.android.material.tabs.TabLayout>
this is java class code
mBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int TabPosition = tab.getPosition();
TabPos = TabPosition + 1;
readBEP("LIST", TabPos+"");
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
this is I want to
enter image description here

Change tab font size when selected/unselected in TabLayout Android

I want to change my tab text size when selected or unselected like this.
My tablayout
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:overScrollMode="never"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="scrollable"
app:tabRippleColor="#null"
app:tabTextAppearance="#style/MyCustomTabText"
app:tabIndicatorHeight="0dp"
/>
MyCustom style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomTabText">
<item name="android:textSize">16sp</item>
</style>
<resources>
I use the custom style for text size but I do not know how to change when it is selected/unselected. Could someone give me a hand with this, please?
To change the Tab font size based the selected/unselected state you have to use your custom tab view and use TabLayout.OnTabSelectedListener to change the size of selected/unselected tab.
1.TabLayout in xml can be like below:
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabGravity="fill"
app:tabIndicatorColor="#android:color/holo_orange_light">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 3" />
</com.google.android.material.tabs.TabLayout>
2.Initialise for each tab a CustomView using setCustomView method and use TabLayout.OnTabSelectedListener to listen which tab is currently selected and which one is now unselected and change the text size like below:
//get each tab from tabLayout
TabLayout.Tab tab0 = tabLayout.getTabAt(0);
TabLayout.Tab tab1 = tabLayout.getTabAt(1);
TabLayout.Tab tab2 = tabLayout.getTabAt(2);
//and set for each one a custom View
tab0.setCustomView(createCustomTabView("Tab 0", 30, android.R.color.holo_green_light)); //initially this tab is selected
tab1.setCustomView(createCustomTabView("Tab 1", 15, android.R.color.black));
tab2.setCustomView(createCustomTabView("Tab 2", 15, android.R.color.black));
//add OnTabSelectedListener
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
setTabTextSize(tab, 30, android.R.color.holo_green_light);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
setTabTextSize(tab, 15, android.R.color.black);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
with the below helper functions to create the Tab CustomView and to change the text size based the selected/unselected state:
private View createCustomTabView(String tabText, int tabSizeSp, int textColor){
View tabCustomView = getLayoutInflater().inflate(R.layout.tab_customview, null);
TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
tabTextView.setText(tabText);
tabTextView.setTextSize(tabSizeSp);
tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor));
return tabCustomView;
}
private void setTabTextSize(TabLayout.Tab tab, int tabSizeSp, int textColor){
View tabCustomView = tab.getCustomView();
TextView tabTextView = tabCustomView.findViewById(R.id.tabTV);
tabTextView.setTextSize(tabSizeSp);
tabTextView.setTextColor(ContextCompat.getColor(tabCustomView.getContext(), textColor));
}
3.And the custom Tab layout R.layout.tab_customview can be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="#+id/tabTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textSize="15sp"
android:textAlignment="center"
android:textColor="#android:color/black"
android:maxLines="1"
tools:text="Tab"/>
</RelativeLayout>
Result:

android - TabLayout dynamically resize

I have a TabLayout in my app and it holds custom views:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<com.myproject.app.utils.commons.IconImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginRight="14dp"
android:layout_marginEnd="14dp"
app:iconStateListColor="#color/color_order_tab"/>
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/color_order_tab"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"/>
</LinearLayout>
</RelativeLayout>
I want to show text on the Tab only if it selected - already done - and resize these tabs dynamically, is there any way to do that?
If I use app:tabMode="fixed" tabs don't resize, if I use app:tabMode="scrollable" my tabs are always on the left of my screen. If I do android:layout_width="wrap_content" and android:layout_gravity="center_horizontal" - TabLayout doesn't fill the screen..
So I need to:
Resize tabs when text appears or disappears;
Tabs should fill the screen in any case.
First of all, you cannot obtain all the desired feature on using fixed mode. As in fixed mode, you cannot resize your tabs.
You need to use scrollable mode in order to resize the tabs.
If I do android:layout_width="wrap_content" and
android:layout_gravity="center_horizontal" - TabLayout doesn't fill
the screen
You can set a parent container and set a background so that it gives you a feel of a tab layout with full device width.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabGravity="center"
app:tabMode="scrollable" />
</FrameLayout>
Then, you can add the tabs i.e. your custom tabs in your tab layout and after that, you can add addOnTabSelectedListener to get the desired results:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
View view = getLayoutInflater().inflate(R.layout.custom_tab, null);
((TextView) view.findViewById(R.id.text)).setText("large text one");
View view1 = getLayoutInflater().inflate(R.layout.custom_tab, null);
((TextView) view1.findViewById(R.id.text)).setText("1");
View view2 = getLayoutInflater().inflate(R.layout.custom_tab, null);
((TextView) view2.findViewById(R.id.text)).setText("Large Text Text Text");
View view3 = getLayoutInflater().inflate(R.layout.custom_tab, null);
((TextView) view3.findViewById(R.id.text)).setText("One");
View view4 = getLayoutInflater().inflate(R.layout.custom_tab, null);
((TextView) view4.findViewById(R.id.text)).setText("Another large text");
mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view));
mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view1));
mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view2));
mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view3));
mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view4));
mBinding.tabs.getTabAt(0).getCustomView().findViewById(R.id.text).setVisibility(View.VISIBLE);
mBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getCustomView().findViewById(R.id.text).setVisibility(View.VISIBLE);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getCustomView().findViewById(R.id.text).setVisibility(View.GONE);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
As you can see the tabs are now resizable and only text on the selected tab is visible. Hope that help
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white"
app:tabTextAppearance="?attr/tabTextAppearance"
app:tabMode="scrollable" />

Android - How to properly show a fragment when a radio button is checked by default?

I'm trying to make an app using fragments, based on this docs: Fragments.
As far as I can understand, I need:
Host Activity [DONE]:
This is my host activity (.java)
public class SphereSurfaceArea extends AppCompatActivity
{
RadioGroup rg_sa;
RadioButton rb_grad, rb_gvol, rb_gcirc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sphere_surface_area);
rg_sa = (RadioGroup) findViewById(R.id.rg_sphere_sa);
rb_grad = (RadioButton) findViewById(R.id.rb_given_rad);
rb_gvol = (RadioButton) findViewById(R.id.rb_given_vol);
rb_gcirc = (RadioButton) findViewById(R.id.rb_given_circ);
rb_grad.setChecked(true); //Here the radio button is checked
rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.rb_given_rad:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new GivenRad());
fragmentTransaction.commit();
break;
}
}
});
}
}
This is my host XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_sphere_surface_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dev.indie.nayir55.geometrycompanion.SolidShapesOperations.SphereOperations.SphereSurfaceArea">
<RadioGroup
android:id="#+id/rg_sphere_sa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!--This is the button checked by default-->
<RadioButton
android:id="#+id/rb_given_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given radious"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"
/>
<RadioButton
android:id="#+id/rb_given_vol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given volume"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"/>
<RadioButton
android:id="#+id/rb_given_circ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal|center|start"
android:text="Given circumference"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="18sp"/>
</RadioGroup>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rg_sphere_sa">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Fragment(s) [DONE]:
This is my fragment .java code:
public class GivenRad extends Fragment
{
public GivenRad()
{
// Required empty public constructor
}
public static GivenRad newInstance()
{
return new GivenRad();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_given_rad, container, false);
return rootView;
}
}
This is my fragment XML layout:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dev.indie.nayir55.geometrycompanion.Fragments.SolidGeometry.FragSphere.GivenRad">
<TextView
android:text="Radious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView9"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"/>
</android.support.constraint.ConstraintLayout>
ID to the container [DONE]:
Inside my host XML layout you'll find the container:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rg_sphere_sa">
</FrameLayout>
A way to dynamically use fragments [DONE]:
Here I'm trying to use fragments dynamically using the radio buttons and a switch:
rg_sa.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.rb_given_rad:
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new GivenRad());
fragmentTransaction.commit();
break;
}
}
});
So... my problem is this:
Using a radio group and radio buttons I set a radio button checked by default, and when I open the host activity containing the radio buttons and the fragment, the radio button is checked but it is not showing the fragment until I check again the button, also if I use if statement is the same result what am I doing wrong?
At the moment, you are only loading a fragment in response to an event in onCheckedChanged(). You also need to load the appropriate fragment in onCreate(). To start, you might want to just copy and paste the code which loads a fragment directly into onCreate(). Eventually you can reduce duplicated code by putting the fragment-loading code in a method (i.e. loadFragment()) which is called from onCreate() and from onCheckedChanged().
What is happening is that you are checking the radio by default, and only loading fragments if the radio is changed or pressed. I would suggest loading the default fragment at runtime to avoid having to hit the button to get it to show up.
I will answer my own question
Here is another approach of the desired behavior I was looking for
Change a fragment
Hope this helps

Multi expandable recyclerview

i try to implements my own multi expandable RecyclerView it'w work fine for one sub-item but when i want sub-sub-item the view display but i need to click on it several times.
Example of hierarchy :
Title1
SubTitle1
SubTitle2
SubTitle3
SubSubTitle1
SubSubTitle2
Title2
...
EDIT : The problem is only on the SubTitle to display SubSub'sItem. I think i need to notify parent adapter that my view changed but i doesn't work to.
To do that i used RecyclerView with itemLayout who contain too an RecyclerView and when i click on item i set adapter off the child RecyclerView. I use custom LinearLayoutManager i found here https://stackoverflow.com/a/29261667/3289338.
My onBindViewHolder :
#Override
public void onBindViewHolder(final DrawerViewHolder holder, final int position) {
if (holder.type == TYPE_EXPANDABLE_ITEM) {
final ItemDrawer item;
if (header) {
item = itemDrawers.get(position - 1);
} else {
item = itemDrawers.get(position);
}
holder.textView.setText(item.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (item.isOpen()) {
item.setOpen(false);
holder.recyclerView_child.setVisibility(View.GONE);
notifyItemChanged(position);
} else {
item.setOpen(true);
holder.recyclerView_child.setVisibility(View.VISIBLE);
holder.recyclerView_child.setAdapter(new DrawerAdapter(item.getSubItem(), drawer, fragmentManager, false));
holder.recyclerView_child.setLayoutManager(new MyLinearLayoutManager(holder.itemView.getContext().getApplicationContext()));
notifyItemChanged(position);
}
}
});
} else if (holder.type == TYPE_SIMPLE_ITEM) {
final ItemDrawer item;
if (header) {
item = itemDrawers.get(position - 1);
} else {
item = itemDrawers.get(position);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (item.isActivity()) {
holder.itemView.getContext().startActivity(item.getActivityIntent());
} else {
fragmentManager.beginTransaction().replace(R.id.drawer_frame_layout, item.getFragment()).commit();
}
}
});
} else {
//header
}
}
And the layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/imageView_drawer_row_expandable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:src="#drawable/ic_drawer" />
<TextView
android:id="#+id/textView_drawer_row_expandable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/imageView_drawer_row_expandable"
android:paddingStart="12dp"
android:paddingTop="4dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="#+id/imageView_drawer_row_expandable_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:paddingEnd="16dp"
android:src="#android:drawable/arrow_up_float" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_drawer_child"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingStart="16dp"
android:scrollbars="vertical"
android:visibility="gone" />
</LinearLayout>
Thanx.
https://github.com/itsnothingg/RecursiveRecyclerView
I made a open source for this.
you can easily implement multi-expanding recyclerview and use different layout for each expanding depth.
i've got same issue, solution is set setHasFixedSize(false) to your parent adapter
NOTE: im using vanilla LinearLayoutManager

Categories

Resources