从eclipse转到android studio的时候新接触一个ConstraintLayout,在使用的过程发现ListView嵌套在ConstraintLayout里面老是出现滑不动或者显示不全的问题,后面怎么修改都有问题,因为要赶项目,索性就不用它,换成了LinearLayout.这次项目告一段落了,就做个调试,小总结。
以前在使用ListView的时候发生滑不动或者显示不全的时候一般就是网上的两种解决方法
/**
* 动态设置ListView的高度
* @param listView
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
if(listView == null) return;
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++)
{
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
listView.setLayoutParams(params);
}
和重定义一个MyListView
/**
* @version 创建时间:2017-11-16 下午4:35:55
* 类说明
*/
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) {
//重新设置高度
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
直接使用ConstraintLayout嵌套Listview会出现最后一个item显示不全的情况,像下面这样,并且如果listview的下面还有其他布局,也不能看到
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="listView测试"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tv_title">
</ListView>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试看listview下面还有布局的情况"
app:layout_constraintTop_toBottomOf="@id/listview" />
<TextView
android:id="@+id/tv_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button下面的测试"
app:layout_constraintTop_toBottomOf="@id/btn_test" />
</android.support.constraint.ConstraintLayout>
如果只用setListViewHeightBasedOnChildren方法只能滑动一点点,不能完全显示,如果只用MyListView或者同时用两种方法则完全不能滑动了。
最后测试的结果发现,最好的解决办法就是在ConstraintLayout的外层嵌套一个ScrollView并且同时采用MyListView才能同时解决所有问题,像下面这样
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="listView测试"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.test.one.myapplication.MyListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tv_title">
</com.test.one.myapplication.MyListView>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试看listview下面还有布局的情况"
app:layout_constraintTop_toBottomOf="@id/listview" />
<TextView
android:id="@+id/tv_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button下面的测试"
app:layout_constraintTop_toBottomOf="@id/btn_test" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
如果只用ScrollView+ListView则会出现listview只显示一个item的情况,如果用ScrollView+ListView+setListViewHeightBasedOnChildren方法会出现listview的最后一个item显示不全的情况,ScrollView+MyListView+setListViewHeightBasedOnChildren方法也可以正常显示,但是,既然ScrollView+MyListView已经可以解决问题,那就没必要再加setListViewHeightBasedOnChildren方法了。而且这个方法可能也有什么问题,才会导致有时出现最后一个item显示不全的情况,具体的由于本人技术不精,也就没去探索了,希望懂的大神留言指教一下,在这里只是跟大家分享总结一下而已。后也发现把ConstraintLayout换成linelayout也是有类似的问题的,所以也是类似的解决办法就行了。