ListView滑不動或者顯示不全和其他一些問題總結

從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也是有類似的問題的,所以也是類似的解決辦法就行了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章