Android RecyclerView關於item無法充滿屏幕的問題

今天寫了一個RecyclerView,寫完後發現item居然不能充滿屏幕
其中,item的佈局是這樣的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

可是,在實際效果中,item並沒有充滿,match_parent完全沒有效果,效果是wrap_content自適應
後面仔細看了代碼,發現是在適配器中加載佈局時候的問題
一開始是這樣寫的,在onCreateViewHolder()中

view = layoutInflater.inflate(R.layout.fragment_notice_item,null);

後面將這代碼改成

view = layoutInflater.inflate(R.layout.fragment_notice_item,parent,false);

就沒有出現item顯示不能充滿的問題了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章