日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题

發布時間:2023/12/15 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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加载布局不全的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。