解决ScrollView与ListView显示冲突问题

来源:互联网 发布:保姆偷喝母乳 知乎 编辑:程序博客网 时间:2024/05/17 02:55
  1. 自定义ListView 重写其onMeasure(),方法
public class ListViewForScrollView extends ListView{    public ListViewForScrollView(Context context, AttributeSet attrs,            int defStyleAttr) {        super(context, attrs, defStyleAttr);        // TODO Auto-generated constructor stub    }    public ListViewForScrollView(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    public ListViewForScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}

2.布局文件引用

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/act_sv"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="\n上方数据\n" />        <com.crazyandroid.custom.ListViewForScrollView             android:id="@+id/act_lv"            android:layout_width="fill_parent"            android:layout_height="wrap_content">        </com.crazyandroid.custom.ListViewForScrollView>        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="\n下方数据\n" />    </LinearLayout></ScrollView>

3.java代码

mScrollView = (ScrollView) findViewById(R.id.act_sv);mScrollView.smoothScrollTo(0, 0);//确保ScrollView从顶端显示

4.更多解决方法 请参阅:
http://www.apkbus.com/forum.php?mod=viewthread&tid=161576

0 0
原创粉丝点击