(转)ScrollView中嵌套ListView置顶问题

原地址:https://blog.csdn.net/u011615817/article/details/46222959


  1. public class MyListView extends ListView {  
  2.     public MyListView(Context context) {  
  3.         super(context);  
  4.     }  
  5.     public MyListView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.     }  
  8.     public MyListView(Context context, AttributeSet attrs, int defStyle) {  
  9.         super(context, attrs, defStyle);  
  10.     }  
  11.     @Override  
  12.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  13.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  14.                 MeasureSpec.AT_MOST);  
  15.         super.onMeasure(widthMeasureSpec, expandSpec);  
  16.     }  
  17. }  



自定义了ListView,在ScrollView中总是置顶显示,不管ListView上面是否有其它view。


解决一:

手动设置ScrollView:

[html]  view plain  copy
  1. ScrollView s = (ScrollView) view.findViewById(R.id.scrollView);  
  2.      s.smoothScrollTo(0, 0);  

解决二:无需手动设置

在ScrollView中需要置顶控件添加以下属性

[html]  view plain  copy
  1. android:focusableInTouchMode="true"  
  2.               android:focusable="true"

猜你喜欢

转载自blog.csdn.net/duyiqun/article/details/80334892