srcollview 嵌套ListView ListView 再嵌套gridview 焦点滑动问题

在Scrollview 中嵌套重写的listview  重写的ListView 再嵌套了重写的gridview 


这是重写的lsitview   出处很多 记录下来方便使用

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);
	}

	// 原理通知系统重新测量大于2的Item高度(默认只会显示前两个item)
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
}

这是重写的gridview  出处很多 记录下来方便使用

public class GridForScrollView extends GridView {
   public GridForScrollView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }
   
   public GridForScrollView(Context context) {
       super(context);
   }
   
   public GridForScrollView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }
   
   @Override
   public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int expandSpec = MeasureSpec.makeMeasureSpec(
               Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, expandSpec);
   }
   
}  

当我这样实现的时候便出现了warn 并且界面无法正常滑动

W/View: requestLayout() improperly called by GridForScrollView{194ae021 VFED..C. .F...... 30,102-1005,342 #7f0d01d9 app:id/gridscrollviews} during second layout pass: posting in next frame

W/View: requestLayout() improperly called by GridForScrollView{fb1ac46 VFED..C. ......ID 30,102-1005,342 #7f0d01d9 app:id/gridscrollviews} during layout: running second layout pass


后来查了资料才发现,有可能是gridview刷新数据使得ListView不断获取焦点 焦点异常造成的滑动异常(不知道是不是这样的)

解决办法 : 在ListView的item中的最外层Layout设置属性 :

android:descendantFocusability="blocksDescendants"

这个属性可以解决父容器和子控件的焦点问题


猜你喜欢

转载自blog.csdn.net/qq_30837235/article/details/50998218