RecyclerView隱藏整個Item后,該Item還占位問題

??今天在寫一個多種類型的列表時,有的列表類型需要根據一些條件來顯示、隱藏,很自然會先獲取到這個Item的最外層布局,然后 v.setVisibility(View.GONE) 進行隱藏,結果Item內容是不顯示了,但是卻占著原先Item的高度。

解決方法:可以給Item對應的layout布局文件的最外層再加上一層布局,這樣當外層布局的高度為wrap_content時,隱藏里面的布局,那么Item的高度就為0了,下面附上相應的代碼:

設置GONE后依舊占位的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cl_container"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:textColor="#FF333333"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Add Friends" />

</android.support.constraint.ConstraintLayout>

修改后,設置GONE后不占位的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_container"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="#FFFFFF">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textColor="#FF333333"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Add Friends" />

    </android.support.constraint.ConstraintLayout>

</FrameLayout>

注:
1.最外層布局FrameLayout不要有layout_marginTop/layout_marginBottom,可放到ConstraintLayout里面設置,否則會占相應的高度;
2.增加一層布局層級肯定會帶來一點資源的消耗,但是鑒于FrameLayout是比較輕量級的,綜合考慮的話這種方式還是不錯的,至于要是本身布局的層級比較多的話,可以考慮重寫xml布局文件,將根布局改為ConstraintLayout,很多需求基本的層級都可以縮小到1至2層,而且ConstraintLayout也是比較高效的,更多的關于ConstraintLayout就不再展開。

另,也有人提出下面的解決方式,在bindView的時候設置Item的高度,也是可行的,附上代碼:

class MyViewHolder extends RecyclerView.ViewHolder {

    MyViewHolder(View itemView) {
        super(itemView);
    }

    public void setVisibility(boolean visible) {
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        if (visible) {
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT; // 根據具體需求場景設置
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        } else {
            itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        }
        itemView.setLayoutParams(param);
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。