ScrollView包裹ListView

来源:互联网 发布:成都oppo 知乎 编辑:程序博客网 时间:2024/04/20 07:55

大家都知道ScrollView只能有一个“孩子”,而且纵向滚动的,同样的ListView也是纵向滚动的,当ScrollView包裹ListView时,是会出现冲突的,那么我们怎么解决呢,我这里提供了最简单的方法:

下面是我做的一个ListView后添加了一个TextView,但TextView要跟着ListView条目的滚动而滚动

一,自定义MyListView类继承ListView,并重写三个有参构造方法和onMeasure方法


package com.bawei.day16_scrollivew;

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

public class MyListView extends ListView {

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 重新计算ListView的高度
        heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

  

二,设置布局


<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" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <com.bawei.day16_scrollivew.MyListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.bawei.day16_scrollivew.MyListView>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#ff0" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>


三,为ListView定义一些假数据,就可以运行了

package com.bawei.day16_scrollivew;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    String[] names=new String[]{
            "1","2","3","4","5","6","7","8","1","2","3","4","5","6","7","8","1","2","3","4","5","6","7","8"
            ,"1","2","3","4","5","6","7","8","1","2","3","4","5","6","7","8","1","2","3","4","5","6","7","8"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyListView listview=(MyListView) findViewById(R.id.listview);
        listview.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_expandable_list_item_1, names));
    }
}


0 0
原创粉丝点击