Last active
February 15, 2024 06:32
-
-
Save f2face/01c8a7b089d2f74748bd12440b14dacc to your computer and use it in GitHub Desktop.
Android GridView with auto expanding height.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#eeeeee" | |
tools:context=".WhateverActivity"> | |
<android.support.design.widget.BottomNavigationView | |
android:id="@+id/navigation" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="0dp" | |
android:layout_marginStart="0dp" | |
android:background="?android:attr/windowBackground" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:menu="@menu/navigation" /> | |
<ScrollView | |
android:id="@+id/scrollView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
app:layout_constraintBottom_toTopOf="@+id/navigation" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent"> | |
<android.support.constraint.ConstraintLayout | |
android:id="@+id/linearLayout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<com.f2face.app.example.Components.ExpandableHeightGridView | |
android:id="@+id/my_grid_view" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:gravity="center" | |
android:horizontalSpacing="2dp" | |
android:numColumns="3" | |
android:verticalSpacing="2dp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.0" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="parent" /> | |
</android.support.constraint.ConstraintLayout> | |
</ScrollView> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.f2face.app.example.Components; | |
/* This component class is based on an answer by @tacone on StackOverflow | |
* Link: https://stackoverflow.com/a/8483078 | |
*/ | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.ViewGroup; | |
import android.widget.GridView; | |
public class ExpandableHeightGridView extends GridView | |
{ | |
boolean expanded = false; | |
public ExpandableHeightGridView(Context context) | |
{ | |
super(context); | |
} | |
public ExpandableHeightGridView(Context context, AttributeSet attrs) | |
{ | |
super(context, attrs); | |
} | |
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) | |
{ | |
super(context, attrs, defStyle); | |
} | |
public boolean isExpanded() | |
{ | |
return expanded; | |
} | |
@Override | |
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) | |
{ | |
// HACK! TAKE THAT ANDROID! | |
if (isExpanded()) | |
{ | |
// Calculate entire height by providing a very large height hint. | |
// View.MEASURED_SIZE_MASK represents the largest height possible. | |
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, | |
MeasureSpec.AT_MOST); | |
super.onMeasure(widthMeasureSpec, expandSpec); | |
ViewGroup.LayoutParams params = getLayoutParams(); | |
params.height = getMeasuredHeight(); | |
} | |
else | |
{ | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
public void setExpanded(boolean expanded) | |
{ | |
this.expanded = expanded; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.f2face.app.example; | |
/* imports .............. */ | |
import com.f2face.app.example.Components.ExpandableHeightGridView; | |
public class WhateverActivity extends AppCompatActivity { | |
ExpandableHeightGridView myGridView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_whatever); | |
ExpandableHeightGridView myGridView = (ExpandableHeightGridView) findViewById(R.id.my_grid_view); | |
// Add this line: | |
myGridView.setExpanded(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment