今天寫了一個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顯示不能充滿的問題了