GridView实现

layoutChildrenView(){
int childrenCount = listData.size();

int singleWidth = (totalWidth - gap * (3 - 1)) / 3;
int singleHeight = singleWidth;

//根据子视图数量上限
ViewGroup.LayoutParams params = getLayoutParams();
params.height = singleHeight * rows + gap * (rows - 1);
setLayoutParams(params);

for (int i = 0; i < childrenCount; i++) {
CustomImageView childrenView = (CustomImageView) getChildAt(i);
childrenView.setImageUrl(((Image) listData.get(i)).getUrl());
int[] position = findPosition(i);
int left = (singleWidth + gap) * position[1];
int top = (singleHeight + gap) * position[0];
int right = left + singleWidth;
int 底部 = 顶部 + singleHeight;

childrenView.layout(左、上、右、下);
}public void setImagesData(List<Image> 列表) {
if (lists == null ||lists.isEmpty()) {
返回;
}
//初始化布置
generateChildrenLayout(lists.size());
//这里做1个重用view的处理
if (listData == null) {
int i = 0;
而 (i < 列表.大小()) {
CustomImageView iv = generateImageView();
addView(iv,generateDefaultLayoutParams());
我++;
}
} 别的 {
int oldViewCount = listData.size();
int newViewCount = list.size();
如果 (oldViewCount > newViewCount) {
removeViews(newViewCount - 1, oldViewCount - newViewCount);
} else if (oldViewCount < newViewCount) {
for (int i = 0; i < newViewCount - oldViewCount; i++) {
CustomImageView iv = generateImageView();
addView(iv,generateDefaultLayoutParams());
}
}
}
listData = 列表
layoutChildrenView();

  1. 包 com.weixinninegridlayout;
  2. 导入 android.content.Context;
  3. 导入 android.graphics.Color;
  4. 导入 android.graphics.drawable.ColorDrawable;
  5. 导入 android.util.AttributeSet;
  6. 导入 android.view.View;
  7. 导入 android.view.ViewGroup;
  8. 导入 android.widget.ImageView;
  9. 导入 java.util.List;
  10. /**
  11. * 由 Pan_ 于 2015/2/2 创建。
  12. */
  13. 公共类 NineGridlayout 扩展 ViewGroup {
  14.     /**
  15.      * 图片之间的间隔
  16.      */
  17.     私人 int 差距 = 5;
  18.     私有整数列//
  19.     私有整数行;//
  20.     私人列表列表数据;
  21.     私有整数总宽度
  22.     公共 NineGridlayout(上下文上下文){
  23.         超级(上下文)
  24.     }
  25.     公共 NineGridlayout(上下文上下文,AttributeSet attrs){
  26.         超级(上下文属性);
  27.         ScreenTools screenTools=ScreenTools.instance(getContext());
  28.         totalWidth=screenTools.getScreenWidth()-screenTools.dip2px(80);
  29.     }
  30.     @覆盖
  31.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  32.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  33.     }
  34.     @覆盖
  35.     protected void onLayout(boolean 改变int l,int t,int rint b) {
  36.     }
  37.     私有无效 layoutChildrenView(){
  38.         int childrenCount = listData.size();
  39.         int singleWidth = (totalWidth - gap * (3 - 1)) / 3;
  40.         int singleHeight = singleWidth;
  41.         //根据子视图数量上限
  42.         ViewGroup.LayoutParams params = getLayoutParams();
  43.         params.height = singleHeight * rows + gap * (rows - 1);
  44.         setLayoutParams(params);
  45.         for (int i = 0; i < childrenCount; i++) {
  46.             CustomImageView childrenView = (CustomImageView) getChildAt(i);
  47.             childrenView.setImageUrl(((Image) listData.get(i)).getUrl());
  48.             int[] position = findPosition(i);
posted @ 2021-08-26 09:14  v17166570219  阅读(46)  评论(0)    收藏  举报