Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题
咱們先看下item的xml布局高度為64dp
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="64dp"android:layout_marginTop="1dp"android:background="@color/white"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_left_delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_centerInParent="true"android:src="@drawable/wpk_user_delete_icon" /><TextViewandroid:id="@+id/tv_schedule_status"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_toRightOf="@id/iv_left_delete"android:singleLine="true"android:tag="@string/ttnormspro_normal"android:text="Turn on at sunset, turn off at 6:00, nex..."android:textSize="17sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/tv_schedule_status"android:layout_toRightOf="@id/iv_left_delete"android:singleLine="true"android:tag="@string/ttnormspro_normal"android:text="S M T W T F S"android:textSize="15sp" /> </RelativeLayout>老套路看圖:
1.在RecyclerView使用下面方法加載布局,第二個參數為null,第三個參數任意
@NonNull @Override public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, true);View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, false);return new EditScheduleHolder(view); }LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, false);
LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, null, true);
?
2.再看下在RecyclerView使用下面方法加載布局,第二個參數為RecyclerView中的ViewGroup,第三個參數為false
@NonNull @Override public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, parent, false);return new EditScheduleHolder(view); }?
3.再看下將上面步驟2中的第三個參數false改為true會怎么樣?
@NonNull @Override public EditScheduleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_edit_schedule_item, parent, true);return new EditScheduleHolder(view); }報錯如下:
ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)總結如下:
1.當我們使用方法一參數加載時候當第二個參數為null,后面參數任意的時候此時的item自己的寬高大小沒有效果因為外層沒有父布局包裹
2.當我們使用方法二參數的時候第二個參數為ViewGroup作為父布局,此時item的高度才會生效,但是第三個參數得為false,如果為true則會報錯如上!
可以看下Google官方參數解析:LayoutInflater.from(context).inflate()
總結
以上是生活随笔為你收集整理的Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为3月新品曝光:手机、耳机、平板扎堆发
- 下一篇: 暗黑三国风!《卧龙:苍天陨落》正式上线: