当前位置: 移动技术网 > IT编程>开发语言>Java > [Android] Android最简单ScrollView和ListView滚动冲突解决方案

[Android] Android最简单ScrollView和ListView滚动冲突解决方案

2019年04月05日  | 移动技术网IT编程  | 我要评论
[Question]问题描述: 单独的ListView列表能自动垂直滚动,但当将ListView嵌套在ScrollView后,会和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>

 

本博客地址:

本文原文地址:

转载请著名出处!谢谢~~

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网