fedilab-Android-App/app/src/main/java/app/fedilab/android/helper/ExpandableHeightListView.java

54 lines
1.6 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.helper;
2017-09-03 10:36:27 +02:00
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
/**
* Created by Thomas on 03/09/2017.
* Expanded listview
*/
2019-09-06 17:55:14 +02:00
public class ExpandableHeightListView extends ListView {
2017-09-03 10:36:27 +02:00
2019-09-06 17:55:14 +02:00
boolean expanded = false;
2017-09-03 10:36:27 +02:00
2019-09-06 17:55:14 +02:00
public ExpandableHeightListView(Context context) {
super(context);
}
2017-09-03 10:36:27 +02:00
2019-09-06 17:55:14 +02:00
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
2017-09-03 10:36:27 +02:00
2019-09-06 17:55:14 +02:00
public ExpandableHeightListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
2017-09-03 10:36:27 +02:00
2019-09-06 17:55:14 +02:00
public boolean isExpanded() {
2017-09-03 10:36:27 +02:00
return expanded;
}
2019-11-15 16:32:25 +01:00
public void setExpanded(@SuppressWarnings("SameParameterValue") boolean expanded) {
this.expanded = expanded;
}
2017-09-03 10:36:27 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2017-09-03 10:36:27 +02:00
// HACK! TAKE THAT ANDROID!
2019-09-06 17:55:14 +02:00
if (isExpanded()) {
2017-09-03 10:36:27 +02:00
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
2019-09-06 17:55:14 +02:00
} else {
2017-09-03 10:36:27 +02:00
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}