类似微信查看群所有成员的动态伸展GridView,ScrollView做父控件(最后增加一个可添加的图片)

来源:互联网 发布:eclipse打包java程序 编辑:程序博客网 时间:2024/05/17 07:55

1:给gridView设置数据后,开始计算gridView的高度;


2:为了使看的一目了然,我就不获取gridView 的列数了,用一个4来代替;


3:增加一个数据,并为其增加一个索引:

                UserInfo info = new UserInfo();
                info.setDPMLastMenber(true);
                userInfos.add(info);

然后重新计算高度
    private void countGridViewHeight() {

        int num = userInfos.size();
        int totalHeight = 0;
        if (num < 4) {
            View itemView = mAdapter.getView(0, null, mGridView);
            itemView.measure(0, 0);
            totalHeight += itemView.getMeasuredHeight();
        } else {
            int result = num / 4;
            for (int i = 0; i < result; i++) {
                View itemView = mAdapter.getView(i, null, mGridView);
                itemView.measure(0, 0);
                totalHeight += itemView.getMeasuredHeight();
            }
            if (num % 4 > 0) {
                totalHeight += totalHeight / result;
            }
        }

        ViewGroup.LayoutParams params = mGridView.getLayoutParams();
        params.height = totalHeight;
        mGridView.setLayoutParams(params);
    }


4:然后就在adapter里面操作了:

  重写getItemViewType(position)和getViewTypeCount()的方法

     public int getItemViewType(int position) {
         
            UserInfo info = mAdapter.getItem(position);
            if (info != null && info.isDpm_status()) {
                return 1;
            }

            return 0;
        }

    public int getViewTypeCount() {
            return 2;
        }

  然后就在getview的方法里面判断就行了!!


 





1 0