我正在使用许多地方提供的代码 -
public void setListHeight(ListView list, CustomListPairingAdapter adapter, int screenRatio)
{
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int _hieght = size.y;
if (adapter != null) {
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, list);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = list.getLayoutParams();
totalHeight=totalHeight + (list.getDividerHeight() * (adapter.getCount()));
int desiredHeight=_hieght*screenRatio/100;
if(desiredHeight<=totalHeight)
params.height = desiredHeight;
else
params.height = totalHeight;
list.setLayoutParams(params);
list.requestLayout();
}
}
但是当列表项具有多行textview时它不起作用。如何解决这个问题。有任何想法吗 ? 谢谢你
答案 0 :(得分:0)
int myHeight=20;
LayoutParams par=new RelativeLayout.LayoutParams
(LayoutParams.MATCH_PARENT, myHeight);
//Replace RelativeLayout with the parent layout of list view
ListView t=(ListView)findViewById(R.id.mylist);
t.setLayoutParams(par);
答案 1 :(得分:0)
您可以尝试使用此自定义列表视图,以避免列表中出现多行文本视图问题
<namespace.epicurio.NestedListView
android:id="@+id/listComments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:divider="#000000"
android:dividerHeight="1.0sp">
</namespace.epicurio.NestedListView>
public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}