【选择图片Jar】Adapter的item布局的实现(三)

来源:互联网 发布:乐乎公寓官网电话 编辑:程序博客网 时间:2024/06/04 22:46

1.回到原点

还记得我在第一篇文章中提及要用代码来写布局,来摆脱jar不能打包资源文件的困扰。 今天就是要带来如何用代码写布局文件。

2.布局分析

首先我们看看我们要实现的布局是什么样子的。截取了一个item
这里写图片描述

我们可以看到,实际上是两个ImageView组成,我相信大家用布局来写,不超过一分钟就写完了。但是用代码实现可能就要下一番功夫了~

3.布局实现

  1. 首先编写一个类 继承自RelativeLayout
  2. 重写他的构造方法
public class ChosePhotoItemLayout extends RelativeLayout {    private ImageView mShow;    private ImageView mSelect;    public ChosePhotoItemLayout(Context context) {        this(context, null);    }    public ChosePhotoItemLayout(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public ChosePhotoItemLayout(Context context, AttributeSet attrs,            int defStyle) {        super(context, attrs, defStyle);        this.setLayoutParams(new RelativeLayout.LayoutParams(                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));        initContentView();    }}

3.Ok,我们看到里面有一个initContentView

// CotentView 中只有 初始化两个ImageView    private void initContentView() {        initShowImgaeView();        initSelectImageView();    }

4.先来看看initShowImageView()这个方法:
这个初始化的是那个惊讶大猫的ImageView

    private void initShowImgaeView() {        mShow = new ImageView(getContext());        // 图片居中        mShow.setScaleType(ScaleType.CENTER_CROP);        // 获取屏幕宽度        int screenWidth = ScreenUtil.getScreenWidth(getContext());        // 取屏幕宽度1/3来作为ImageView的大小        int showViewWidth = screenWidth / 3;        // 设置ImageView参数        mShow.setLayoutParams(new ViewGroup.LayoutParams(showViewWidth,                showViewWidth));        // 添加到RelativeLayout父布局中去        this.addView(mShow);    }

相信这段代码也非常好理解。ScreenUtil这个是我封装好的一个工具类,具体代码在最后贴出

5.依葫芦画瓢 我们看看initSelectImageView这个方法

    private void initSelectImageView() {                mSelect = new ImageView(getContext());        // 图片居中显示        mSelect.setScaleType(ScaleType.CENTER_CROP);        // 默认处理        int screenWidth = ScreenUtil.getScreenWidth(getContext());        int selectViewWidth = screenWidth / 18;        RelativeLayout.LayoutParams p = new LayoutParams(selectViewWidth,                selectViewWidth);        // 位于父布局右侧        p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);        // 位于父布局上侧        p.addRule(RelativeLayout.ALIGN_PARENT_TOP);        // 距离        int margin = selectViewWidth / 4;        // 右边距离        p.rightMargin = margin;        // 左边距离        p.topMargin = margin;        this.addView(mSelect, p);    }

相信这段代码也非常好理解。 需要注意的是RelativeLayout.LayoutParams 一定要注意导正确的包,一开始我用了ViewGroup.LayoutParams折腾了很久都 没有成功将箭头成功放在布局的右上角!!

至此布局文件的核心已经写好了~~!!附上完整代码以及ScreenUtil的代码。

package com.hans.selectphoto.view.itemlayout;import com.hans.selectphoto.util.ScreenUtil;import com.nostra13.universalimageloader.utils.L;import android.content.Context;import android.util.AttributeSet;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.RelativeLayout;public class ChosePhotoItemLayout extends RelativeLayout {    private ImageView mShow;    private ImageView mSelect;    public ChosePhotoItemLayout(Context context) {        this(context, null);    }    public ChosePhotoItemLayout(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public ChosePhotoItemLayout(Context context, AttributeSet attrs,            int defStyle) {        super(context, attrs, defStyle);        this.setLayoutParams(new RelativeLayout.LayoutParams(                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));        initContentView();    }    // CotentView 中只有 初始化两个ImageView    private void initContentView() {        initShowImgaeView();        initSelectImageView();    }    private void initSelectImageView() {        mSelect = new ImageView(getContext());        // 图片居中显示        mSelect.setScaleType(ScaleType.CENTER_CROP);        // 默认处理        int screenWidth = ScreenUtil.getScreenWidth(getContext());        int selectViewWidth = screenWidth / 18;        RelativeLayout.LayoutParams p = new LayoutParams(selectViewWidth,                selectViewWidth);        // 位于父布局右侧        p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);        // 位于父布局上侧        p.addRule(RelativeLayout.ALIGN_PARENT_TOP);        // 距离        int margin = selectViewWidth / 4;        // 右边距离        p.rightMargin = margin;        // 左边距离        p.topMargin = margin;        this.addView(mSelect, p);    }    private void initShowImgaeView() {        mShow = new ImageView(getContext());        // 图片居中        mShow.setScaleType(ScaleType.CENTER_CROP);        // 获取屏幕宽度        int screenWidth = ScreenUtil.getScreenWidth(getContext());        // 取屏幕宽度1/3来作为ImageView的大小        int showViewWidth = screenWidth / 3;        // 设置ImageView参数        mShow.setLayoutParams(new ViewGroup.LayoutParams(showViewWidth,                showViewWidth));        // 添加到RelativeLayout父布局中去        this.addView(mShow);    }    public ImageView getShowImageView() {        return mShow;    }    public ImageView getSelectImageView() {        return mSelect;    }}

ScreenUtil的代码:

package com.hans.selectphoto.util;public class ScreenUtil {    public static int getScreenWidth(Context context) {        WindowManager wm = (WindowManager) context                .getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics outMetrics = new DisplayMetrics();        wm.getDefaultDisplay().getMetrics(outMetrics);        return outMetrics.widthPixels;    }    public static int getScreenHeight(Context context) {        WindowManager wm = (WindowManager) context                .getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics outMetrics = new DisplayMetrics();        wm.getDefaultDisplay().getMetrics(outMetrics);        return outMetrics.heightPixels;    }}

好的!至此,我们成功用代码实现布局啦~下面我们就看看如何使用了~

4.使用布局

首先我们来回顾一下我们原来的是如何使用layout下的布局的?只需要:

convertView = LayoutInflater.from(context).inflate(R.id.xx,....);

如何获取布局文件中的控件呢?只需要:

viewHolder.show = (ImageView)     convertView .findViewById(R.id.grid_iv_pic);viewHolder.select = (ImageView)    convertView.findViewById(R.id.grid_iv_button);

就可以成功使用了吧? 那现在我们要如何使用呢?嘿嘿。。

其实只需要new就好了- -#没错就是这么简单 - -#是不是碉堡了。 哈,开个玩笑。

convertView = new ChosePhotoItemLayout(mContext);

那我们获取控件呢?还记得我们对外暴露了两个方法吧?只需要:

viewHolder.show = ((ChosePhotoItemLayout) convertView)                    .getShowImageView();viewHolder.select = ((ChosePhotoItemLayout) convertView)                    .getSelectImageView();

5.结束语

ok! 今天的文章也完毕啦~应该都学会了吧。 编程这种东西还是要自己打过才知道。有很多时候就是因为导错包而导致了解决不了问题。比如我的SelectImageView的那个RelativeLayout.LayoutParams的问题!

好的Adapter的所有的代码也慢慢的浮出水面啦~下一篇文章将带着大家揭开GalleryAdapter神秘面纱!!! 其实一点都不神秘- -#

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机静音找不到了怎么办 论文被别人抄袭怎么办 冒充网警诈骗怎么办 南师大自发挂了怎么办 中学生沉迷手机游戏怎么办? 孩子花钱玩游戏怎么办 mykung充不了电怎么办 脑出血恢复期身体软怎么办 腿里面有积水怎么办 手臂伤筋了怎么办 想打羽毛球没人怎么办 对看书没有兴趣怎么办 痴呆老人晚上闹怎么办 高中毕业证没拿怎么办 高考志愿同分怎么办 儿童吃虾过敏怎么办 滑板湿水了怎么办 老员工不听指挥怎么办 手机显示连接itunes怎么办 苹果手机下载不信任怎么办 暗影格斗3进不去怎么办 喝酒第二天头痛怎么办 加油忘记拔枪怎么办 刷机完成后怎么办 苹果8按键音太小怎么办 电脑蓝屏出英文怎么办 win10蓝屏重启怎么办 苹果手机变蓝屏怎么办 nga账号声望不足怎么办 鬃狮蜥胆小咬人怎么办 小狗拉稀脱水了怎么办 泰迪刚到家老叫怎么办 开车遇到拉死人怎么办 斯诺克解不到球怎么办 颈椎和腰椎不好怎么办 车祸肇事者不赔偿怎么办 交强险自赔吃亏怎么办 交通事故保险不结案怎么办 马蚁借呗超出一天还款怎么办 千里追风油太辣怎么办 蛀牙牙肉肿痛怎么办