ScrollView中嵌套GridView/ListView时只显示一行的解决办法
重写ListView :
/**
* 自定义gridview,解决ListView中嵌套gridview显示不正常的问题
*/
public class MyGridView extends GridView{
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(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);
}
}
重写GridView :
public class MyListView extends ListView {
public MyListView(Context context) {
// TODO Auto-generated method stub
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
// TODO Auto-generated method stub
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
GridView点击不变色 : android:listSelector="@android:color/transparent"
缩放与列宽大小同步 : android:stretchMode="columnWidth"
<span style="white-space:pre"> </span><com.example.jingzhi.utils.MyGridView
android:id="@+id/gv_edu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:listSelector="@android:color/transparent"
android:stretchMode="columnWidth"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" />
ScrollView嵌套ListView时,初始位置错误的问题
解决办法:
1.给父容器设置属性
android:focusable="true"
android:focusableInTouchMode="true"
2.代码中设置
listView.setFocusable(false);