自定义可适应ScrollView的ListView

来源:互联网 发布:mysql安装教程 图解 编辑:程序博客网 时间:2024/05/29 09:07

自定义一个类继承自ListView,通过重写其onMeasure方法,达到对ScrollView适配的效果。

mport android.content.Context;import android.util.AttributeSet;import android.widget.ListView;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);    }}

在xml布局中和Activty中使用的ListView改成这个自定义ListView就行了。

这个方法和方法1有一个同样的毛病,就是默认显示的首项是ListView,需要手动把ScrollView滚动至最顶端。

  • sv = (ScrollView) findViewById(R.id.act_solution_4_sv);
  • sv.smoothScrollTo(0, 0);

0 0
原创粉丝点击