[Android] Android最簡單ScrollView和ListView滾動沖突解決方案


[Question]問題描述:

單獨的ListView列表能自動垂直滾動,但當將ListView嵌套在ScrollView后,會和ScrollView的滾動滑塊沖突,造成ListView滑塊顯示不完整。

 

activity_main.xml表現:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
****此處省略ScrollView的修飾代碼***
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="test"/>

<ListView
android:id="@+id/list_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout>

</ScrollView>

 

 

[Solution]解決方法:

第一步:自定義ListViewForScrollView類:  ListViewForScrollView.java

 

package com.jack.helloen.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * 自定義一個類繼承自ListView,通過重寫其onMeasure方法,達到對ScrollView適配的效果。
 */
public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    /**
     * 重寫該方法,達到使ListView適應ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

 

第二步: MainActivity.java里增加兼容patch

 

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

       //ListView兼容ScrollView
        sv = (ScrollView) findViewById(R.id.main_scroll);
        sv.smoothScrollTo(0, 0);
}

 

第三步:布局文件xml 里 引用自定義 組件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/main_scroll"
*** >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="test"/>


        <com.jack.helloen.ui.ListViewForScrollView
            android:id="@+id/list_class"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"/>
    </LinearLayout>
</ScrollView>

 

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10657559.html

轉載請著名出處!謝謝~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM