写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/129218891. 如果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
- 写RecycleView适配器时,发现inflate的问题
- LayoutInflater.inflate及View.inflate一点小结----(当使用listview,recycleView时发现也有些不同)
- RecycleView适配器的封装
- RecycleView的适配器
- 写JavaScript发现的问题
- recycleview适配器
- Recycleview嵌套Recycleview时出现的一个问题-待解决
- Recycleview的使用以及编写Recycleview适配器的框架-BaseRecyclerViewAdapterHelper
- inflate()参数的问题
- RecycleView的问题笔记
- 万能RecycleView适配器
- 打造RecycleView 通用适配器
- RecycleView适配器 Persener层
- 关于inflate时LayoutParams不生效的问题
- recycleview的复用问题
- NestedScrollView嵌套RecycleView的问题
- 写项目时的一个小发现
- 自己写的一个recycleview下拉加载
- Login函数
- 使用Ajax跳转页面给下个页面传入数值要用session
- Yii2.0页面提示消息
- 天天写「业务代码」,如何成为「技术大牛」?
- POJ 1321 棋盘问题(dfs深搜)
- 写RecycleView适配器时,发现inflate的问题
- Galaxy OJ 六校联测4 T4:切蛋糕(二分答案)
- 【LeetCode】55. Jump Game (Medium)
- 超级简单的选择排序
- 技能树 DP
- matlab利用fzero求零点
- 彻底修改eclipse的默认编码
- windows查看端口占用以及关闭相应的进程
- (M)(Tree)Lowest Common Ancestor of a Binary Tree