Android RecyclerView item视图 、动态添加View时视图宽度问题

一、场景:

当我们在RecyclerView 或者 动态给View添加视图时会使用 LayoutInflater 将视图布局解析成View,然后添加到根布局中。LayoutInflater inflate方法使用不当就会导致视图宽度有问题,比如inflate的布局中给定了宽高,但是最终展示还是充斥整个布局。或者出现java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

二、问题

1、RecyclerView item视图 、动态添加View时视图宽度问题
2、java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

三、解决

使用LayoutInflater.from(parent.context).inflate(R.layout.adapter_navigation, parent,false)同时解决上面两个问题。

四、分析

LayoutInflater inflate 最终调用的都是此方法

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

1、源码片段

ViewGroup.LayoutParams params = null;

 if (root != null) {
        if (DEBUG) {
            System.out.println("Creating params from root: " +
                    root);
        }
        // Create layout params that match root, if supplied
        params = root.generateLayoutParams(attrs);
        if (!attachToRoot) {
            // Set the layout params for temp if we are not
            // attaching. (If we are, we use addView, below)
            temp.setLayoutParams(params);
        }
    }

如果root != null,创建LayoutParams,params = root.generateLayoutParams(attrs) 这就是问题的所在。

    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }
        public LayoutParams(Context c, AttributeSet attrs) {
            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
            setBaseAttributes(a,
                    R.styleable.ViewGroup_Layout_layout_width,
                    R.styleable.ViewGroup_Layout_layout_height);
            a.recycle();
        }
        protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
            width = a.getLayoutDimension(widthAttr, "layout_width");
            height = a.getLayoutDimension(heightAttr, "layout_height");
        }

2、源码片段

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }

注意 root.addView(temp, params); 方法,我们在看ViewGroup的addView方法调用的

private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout)

里面的片段

        if (child.getParent() != null) {
            throw new IllegalStateException("The specified child already has a parent. " +
                    "You must call removeView() on the child's parent first.");
        }

这便是问题根源,那为什么childparent会有值呢?看下面

        // tell our children
        if (preventRequestLayout) {
            child.assignParent(this);
        } else {
            child.mParent = this;
        }
    /*
     * Caller is responsible for calling requestLayout if necessary.
     * (This allows addViewInLayout to not request a new layout.)
     */
    void assignParent(ViewParent parent) {
        if (mParent == null) {
            mParent = parent;
        } else if (parent == null) {
            mParent = null;
        } else {
            throw new RuntimeException("view " + this + " being added, but"
                    + " it already has a parent");
        }
    }

    /**
     * The parent this view is attached to.
     * {@hide}
     *
     * @see #getParent()
     */
    protected ViewParent mParent;

所以当需要连续重复使用同一个布局时 inflate中的第三个参数 boolean attachToRoot 必须设置为false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,可以使用视图绑定(ViewBinding)来替代findViewById方法,以更方便地访问和操作视图元素。下面是使用视图绑定实现视图的步骤: 1. 确保你的项目已经升级到Android Studio 3.6 Canary 11或更高版本。 2. 在项目的build.gradle文件中,将以下代码添加android块中: ```groovy viewBinding { enabled = true } ``` 3. 在布局文件中,使用<layout>标签将布局文件包裹起来,例如: ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 布局内容 --> </LinearLayout> </layout> ``` 4. 在Activity或Fragment中,使用以下代码来获取视图绑定实例: ```java // 对于Activity private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // 现在可以通过binding对象访问布局中的视图元素了 binding.textView.setText("Hello World!"); } // 对于Fragment private FragmentMainBinding binding; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = FragmentMainBinding.inflate(inflater, container, false); View rootView = binding.getRoot(); // 现在可以通过binding对象访问布局中的视图元素了 binding.textView.setText("Hello World!"); return rootView; } ``` 通过使用视图绑定,你可以直接使用布局文件中定义的视图元素,而无需手动调用findViewById方法。这样可以提高代码的可读性和开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值