当前位置:首页 > Android

GridView中item高度自适应

jsc10年前 (2016-04-01)Android4504

item高度自适应

public class MyAdapter extends BaseAdapter {

        GridView mGv;   
    public static int ROW_NUMBER = 5;

    public MyAdapter (GridView gv, Context mContext, ArrayList<String> list) {
        this.context = mContext;
        this.mGv = gv;
        lstDate = list;
    }

       @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
        //高度计算
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                mGv.getHeight()/ROW_NUMBER);
        convertView.setLayoutParams(param);

        return convertView;
    }

关键点  通过获取GridView 的高度来计算每个item的高度。缺点:个人认为,实际上GridView 重绘了一遍(数据量大时影响性能),第一遍的时候GridView高度为0,item也为0,求证!!  如果设置了ViewHolder 会使第一个item缺失。

 mGv.getHeight()/ROW_NUMBER


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/57.html

分享给朋友:

“GridView中item高度自适应” 的相关文章

Android自定义属性,format详解

1. reference:参考某一资源ID。     (1)属性定义:             <declare-styleable name = &qu…

TypedArray和obtainStyledAttributes使用

TypedArray和obtainStyledAttributes使用

在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用......…

java.util.Properties类

java.util.Properties类

一、认识properties文件 1、properties文件是一个文本文件2、properties文件的语法有两种,一种是注释,一种属性配置。 注    释:前面加上#号 属性配置:以“键=值”的方式书写一个属性的配置信息。3、prope…

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

˂p style="font-family:Arial; font-size:14px; line-height:26px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0…

修改keystore密码别名等

修改keystore密码别名等

之前在测试Eclipse ADT的Custom debug ˂a target="_blank" title="View all posts in keystore" hr…

Fragment保持状态切换

Fragment保持状态切换

在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常挂掉网上说使用replace切换会使被切换的Fragment给替换掉,从而被被切换的Fra…