写RecycleView适配器时,发现inflate的问题

来源:互联网 发布:未来网络实验室 编辑:程序博客网 时间:2024/05/21 17:57

问题:在写RecycleView适配器时,发现inflate用不同的方法,UI效果不一样:


item布局就是一个TextView,

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@+id/textview"          android:layout_width="match_parent"          android:layout_height="72dp"          android:background="#55ff0000"          android:gravity="center"          android:orientation="vertical"></TextView>


适配器如下:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {    private List<String> data;    Context mContext;    LayoutInflater mInflater;        public MyAdapter(List<String> data, Context mContext) {        this.mContext=mContext;        this.data=data;        mInflater = LayoutInflater.from(mContext);    }    @Override    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        //inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)        //R.layout.item, null,root!=null        View inflate = View.inflate(mContext, R.layout.item, null);        //View view = mInflater.inflate(R.layout.item, parent,false);        Log.i("at22", "onCreateViewHolder: "+parent);        return new MyViewHolder(inflate);    }    @Override    public void onBindViewHolder(MyViewHolder holder, int position) {        holder.mTextView.setText(data.get(position));    }    @Override    public int getItemCount() {        return data.size();    }    public class MyViewHolder extends RecyclerView.ViewHolder {        TextView mTextView;        public MyViewHolder(View itemView) {            super(itemView);            mTextView = (TextView) itemView.findViewById(R.id.textview);        }    }}


在适配器中常用加载布局方法:

1.View的静态方法 View.inflate

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {    LayoutInflater factory = LayoutInflater.from(context);    return factory.inflate(resource, root);}

内部也是调用了LayoutInflater的方法:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {    return inflate(resource, root, root != null);}

继续跟踪方法调用:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {    final Resources res = getContext().getResources();    if (DEBUG) {        Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("                + Integer.toHexString(resource) + ")");    }    final XmlResourceParser parser = res.getLayout(resource);    try {        return inflate(parser, root, attachToRoot);    } finally {        parser.close();    }}
</pre>
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
//最后都是调用这个方法来解析xml
......
}
一般这么调用的方法:
View inflate = View.inflate(mContext, R.layout.item, null)-----------------------显示效果如图1
可见传入的root为null,
boolean attachToRoot=(root!=null),可见attachToRoot=false;
这种情况下,root=null,attachToRoot=false;
当我们用View的静态方法inflate( )时候,发现attachToRoot=(root!=null)这一点;

2.直接用
mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.item, parent,false);//public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
其中parent就是RecycleView,通过打印发现的
可见传入的root为RecycleView,boolean attachToRoot=false;
最后的效果图如第二个图所示
那么attachToRoot又表示的什么含义呢?/--------来自郭神博客:http://blog.csdn.net/guolin_blog/article/details/12921889

1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。

2. 如果root不为null,attachToRoot设为true,则会给加载的布局文件的指定一个父布局,即root。

3. 如果root不为null,attachToRoot设为false,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效。

4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

对比下效果图:
第一种是在root为null,attachToRoot为false的情况下
第二种是在root不为null,attachToRoot为false的情况下,如果将attachToRoot改为true会报错:
<pre name="code" class="html">java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
在第二种的基础上,将root修改为null,发现attachToRoot修改了,不影响,效果都一样
1,LayoutInflater的内部实现方式,主要是pull解析布局文件,然后通过反射的方式生成控件对象,形成dom树结构;
2,setContentView方法会给xml布局文件添加一个FrameLayout;
3,视图界面都是由两个部分组成:标题栏和内容栏,标题栏是系统自动给我们添加的

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司报销发票纸质的丢了怎么办 在京东上买的小天才手表坏了怎么办 美亚买东西过几天降价了怎么办 天猫买东西退款后又不想退了怎么办 自己写错了地址快递被签收了怎么办 在天猫购买东西不发货怎么办 在天猫买了东西不给我发货怎么办 天猫退货卖家收到货不退款怎么办 唯品会的账号找不回来了怎么办 从网上买的沙发物流超级慢怎么办 三星c5手机玩王者荣耀卡怎么办 买手机送话费的卡不想用了怎么办 到银行存钱被骗买保险了怎么办 去银行存钱被骗买了保险怎么办 京东申请退款后卖家又发货了怎么办 苹果6s没有4g网怎么办 花呗分期付款买手机额度不够怎么办 手机天猫不支持购买淘宝商品怎么办 天猫国际购买商品狠盾怎么办 在微信上微拍堂买东西被骗了怎么办 京东自营买到返修手机怎么办? 7p弯了怎么办能修复吗 农商银行app登录密码忘了怎么办 网址上的重庆时时彩不能提现怎么办 天天中彩票自己账户登录不了怎么办 天猫上卖王者荣耀的兑换码是怎么办 身份证借别人开淘宝永久封店怎么办 天猫店铺被扣12分怎么办 天猫法人变更之前的天猫贷款怎么办 在日本旅游遇到精日导游怎么办 银行资金交易异常卡被冻结怎么办 如果淘宝被盗了店铺乱上东西怎么办 快递不送货直接代售点签收怎么办 淘宝快递没收到却显示已签收怎么办 支付宝余额未满16受限怎么办 未满16岁支付宝余额受限怎么办 手机天猫购物买的数量大怎么办 扣扣游戏领礼包出现账号异常怎么办 美容院转让给别人客人要退钱怎么办 卖家毁约中介费担保费怎么办 天猫买东西店家不开增值税票怎么办