listView 嵌套gridView android
思路:
1.固定gridView ,禁用它的滾動操作(否則會混淆滑動事件)
1.得到gridView 高度,即子控件的內容全部顯示出來,把高度傳給 onMeasure();
3.自定義一個不能滾動的 gridView,並測量
具體的方法是自定義GridView組件,繼承自GridView。重載onMeasure方法:
Java代碼
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/***
* 固定大小不滾動的GridView
*/
public class fixedGridView extends GridView {
Context context;
public fixedGridView(Context context) {
super(context);
this.context =context;
}
public fixedGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context =context;
}
/**
* 設置不滾動
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> Integer.parseInt(context.getString(R.string.numColumns)),MeasureSpec.AT_MOST);//把測量的值傳入onMeasure 重繪佈局的高度
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
其中onMeasure函數決定了組件顯示的高度與寬度;
makeMeasureSpec函數中第一個函數決定佈局空間的大小,第二個參數是佈局模式
MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就擴展到多大的空間
之後在ScrollView中添加這個組件就OK了,同樣的道理,ListView也適用。
同樣也可以
recycleView嵌套GridView
同樣的道理,recycleView嵌套GridView也適用,recycleView嵌套recycleView也適用。在這就不一一列舉了。
效果圖:
demo就5個文件,主要是用的高清圖導致app稍微大了點
ListView嵌套GridView demo地址 : http://download.csdn.net/detail/wanmeirongyan100/9824675
recycleView嵌套GridView demo地址 :http://download.csdn.net/detail/wanmeirongyan100/9824854