If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package org.gskbyte.view;
/*fromwww.java2s.com*/import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
/**
* This class avoids the problem of a List which is too small when you use wrap_content.
*
* A typical use-case for this is to have a ListView that can be embedded in a ScrollView
* (for example, when you want to include a custom header)
*
* @see ExpandedGridView, the source code is adapted from there
*
* Original source http://stackoverflow.com/questions/8481844/gridview-height-gets-cut
* */publicclass ExpandedListView
extends ListView
{
public ExpandedListView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public ExpandedListView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
init();
}
protectedvoid init()
{
setVerticalFadingEdgeEnabled (false);
setHorizontalFadingEdgeEnabled (false);
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
}
privateboolean adapterJustSet = true;
@Override
publicvoid setAdapter(ListAdapter adapter)
{
super.setAdapter(adapter);
adapterJustSet = true;
}
@SuppressLint("DrawAllocation")
@Override
publicvoid onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// 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 = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
getLayoutParams().height = getMeasuredHeight();
if(adapterJustSet) {
ViewParent parent = getParent();
if(parent instanceof ViewGroup) {
ViewParent grandparent = parent.getParent();
if(grandparent instanceof ScrollView) {
final ScrollView sv = ((ScrollView) grandparent);
sv.post(new Runnable() {
publicvoid run() {
sv.scrollTo(0, 0);
}});
}
}
adapterJustSet = false;
}
}
}