》由于ListView,GridView本身都继承于ScrollView,一旦在ScrollView中嵌套ScrollView,在ScrollView中嵌套使用ListView或者GridView,ListView只会显示一行多一点。两者进行嵌套,即会发生冲突。核心解决方案:重写ListView或者GridView的OnMesure方法。重写GridView方法,使其在绘制时重新计算GridView高度
---------------------
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/**
*
* @author 作者 :desaco
*
* @version 创建时间:2016-1-22 上午9:43:33
*
*/
public class CustomGridView extends GridView{
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
----------------------------------
public class MyGridView extends GridView {
private boolean haveScrollbar = true;
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 设置是否有ScrollBar,当要在ScollView中显示时,应当设置为false。 默认为 true
*
* @param haveScrollbars
*/
public void setHaveScrollbar(boolean haveScrollbar) {
this.haveScrollbar = haveScrollbar;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (haveScrollbars == false) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
------------------------
XML代码如下:
<cn.com.example.fang_com.beta_content.iwidgets.CustomGridView
android:id="@+id/common_show_workbenchs_gview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_color"
android:listSelector="#00000000"
android:numColumns="2"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:scrollbars="none" />
---------------------------------
import android.content.Context;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.ListView;
public class TestListView extends ListView{
public TestListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public TestListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TestListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@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);
}
}