在写Android项目中,ListView是必用的,如果是联网的项目,在获取接口之后,就会遇到listview中,只显示一行的大小的问题,里面就是一个滚动条,其他数据会在里面需要滚动才看到,这肯定和我们平时用的app不同,看着就不爽。我们需要的是,listview里面的数据全部显示出来,用外面的ScrollView的滑动条来滚动,这就需要我们自定义Listview了,很简单。首先需要写一个自定义的listview的java文件,再在xml中把listview换成自定义的文件即可。
AutoAdjustHeightListView.java
package com.jobui.app.custom.list;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by Kam on 14-6-22.
*/
public class AutoAdjustHeightListView extends ListView {
public AutoAdjustHeightListView(Context context) {
super(context);
}
public AutoAdjustHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoAdjustHeightListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
fragment_rank.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:id="@+id/rankScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<com.jobui.app.custom.list.AutoAdjustHeightListView
android:id="@+id/company_rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@null"
android:scrollbars="none" />
</ScrollView>