嵌套开发会出现 ListView数据显示一条的问题, 解决办法:
MainActivity:public class MainActivity extends Activity {
private ListView mLv;
private List<String> list;
private ArrayAdapter<String> adpter;//系统适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
initView();
}
private void initList() {
list=new ArrayList<String>();
for (int i = 0; i < 100; i++) {
list.add("sdsadasdd"+i);
}
}
private void initView() {
mLv=(ListView) findViewById(R.id.mLv);
adpter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1 ,android.R.id.text1 , list);
mLv.setAdapter(adpter);
}
}
(一)第一种:添加一个ScrollView里面的一个 filview 属性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.bb.sqiantaolv.MainActivity" >
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/mLv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
(2)第二种:自定义一个ListView
public class MyListView extends ListView{
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int hight=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE<<2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, hight);
}
}
(三)第三种:在JAVA布局写一个获取适配器的高度public class ListViewUtils {
public static void setMeaseHight(ListView lv){
int totalHeight=0;
ListAdapter list = lv.getAdapter();
if (list==null) {
return;
}
if (list.getCount()<=1) {
return;
}
// http://www.cnblogs.com/exmyth/p/4544551.html
// int desiredWidth=MeasureSpec.makeMeasureSpec(lv.getWidth(), MeasureSpec.AT_MOST);
View v=null;
for (int i = 0; i < list.getCount(); i++) {
v = list.getView(i, v,lv);
v.measure(0, MeasureSpec.UNSPECIFIED);
totalHeight=totalHeight+v.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv.getLayoutParams();
params.height=totalHeight+(lv.getDividerHeight()*(list.getCount()-1));
lv.setLayoutParams(params);
// lv.requestLayout();
}
}
好了,问题解决,完美适用任何嵌套