【问题标题】:Recyclerview items not covering entire screenRecyclerview 项目未覆盖整个屏幕
【发布时间】:2021-04-23 20:23:37
【问题描述】:

几天来,我一直在为一个错误而苦苦挣扎。出于某种原因,我的回收站视图项目没有覆盖整个屏幕。 这是代码:

feed_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="viewModel"
            type="com.example.bookally.ui.posts.feed.FeedViewModel" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorWhite"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme">

                <TextView
                    style="@style/MainHeading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/feed"
                    android:textSize="20sp" />

            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/feeds_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/white"
            android:clipToPadding="false"
            android:paddingLeft="16dp"
            android:paddingTop="28dp"
            android:paddingRight="16dp"
            android:paddingBottom="28dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar"
            tools:listitem="@layout/item_feed" />

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:src="@drawable/ic_baseline_add_24"
            android:onClick="@{() -> viewModel.addPosts()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:contentDescription="@string/create_post" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

item_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="post"
            type="com.example.bookally.firebase.PostContent" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@drawable/post_border">

        <ImageView
            android:id="@+id/author_profile_picture"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:contentDescription="@string/user_image"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:setProfilePhoto="@{post.authorImageUrl}" />

        <TextView
            android:id="@+id/author_name"
            style="@style/MainHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:text="@{post.author}"
            android:textSize="18sp"
            app:layout_constraintStart_toEndOf="@+id/author_profile_picture"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Iqbal Singh" />

        <TextView
            android:id="@+id/username"
            style="@style/MainHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="4dp"
            android:text="@{post.username}"
            android:textColor="@color/colorGrey"
            android:textSize="16sp"
            app:layout_constraintStart_toEndOf="@+id/author_profile_picture"
            app:layout_constraintTop_toBottomOf="@+id/author_name"
            tools:text="\@just_another_boy" />

        <TextView
            android:id="@+id/post_content"
            style="@style/SubHeading"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:gravity="start"
            android:text="@{post.content}"
            android:textAlignment="viewStart"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/username"
            tools:text="Anything random ...." />

        <ImageButton
            android:id="@+id/like_btn"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/likes"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/post_image"
            app:likeIcon="@{post.likedBy}" />

        <TextView
            android:id="@+id/likes_no"
            style="@style/MainHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/like_btn"
            app:layout_constraintStart_toEndOf="@+id/like_btn"
            app:layout_constraintTop_toTopOf="@+id/like_btn"
            app:likesText="@{post.likes}"
            tools:text="7 Likes" />

        <TextView
            android:id="@+id/comments_no"
            style="@style/MainHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:textSize="16sp"
            app:commentsText="@{post.commentsNumber}"
            app:layout_constraintStart_toEndOf="@+id/comment_btn"
            app:layout_constraintTop_toTopOf="@+id/comment_btn"
            tools:text="0 Comments" />

        <ImageButton
            android:id="@+id/comment_btn"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/comment"
            android:src="@drawable/ic_comment"
            app:layout_constraintStart_toEndOf="@+id/likes_no"
            app:layout_constraintTop_toBottomOf="@+id/post_image" />

        <ImageButton
            android:id="@+id/save_post_btn"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/save_post"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/post_image"
            app:saveIcon="@{post.savedBy}" />

        <ImageButton
            android:id="@+id/add_friend_btn"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/add_friend"
            android:src="@drawable/ic_add_friend"
            app:layout_constraintBottom_toBottomOf="@+id/username"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/author_name" />

        <ImageView
            android:id="@+id/post_image"
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:contentDescription="@string/post_imageview_content_description"
            app:layout_constraintBottom_toTopOf="@+id/comments_no"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/post_content"
            app:layout_constraintVertical_bias="0.0"
            app:loadImagesFromFirebase="@{post.postImageUrl}" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

FeedAdapter

class FeedAdapter(private val onClickListener: ClickListener)  : PagingDataAdapter<Post, FeedAdapter.FeedViewHolder>(
    DiffCallback
) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
        return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context)))
    }

    override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {
        val post = getItem(position)

        holder.itemView.findViewById<ImageButton>(R.id.like_btn).setOnClickListener { if (post != null)  onClickListener.onLikeClicked(post)  }
        holder.itemView.findViewById<ImageButton>(R.id.comment_btn).setOnClickListener { if (post != null)  onClickListener.onCommentClicked(post)  }
        holder.itemView.findViewById<ImageButton>(R.id.save_post_btn).setOnClickListener { if (post != null) onClickListener.onSavePostClicked(post)  }
        holder.itemView.findViewById<ImageButton>(R.id.add_friend_btn).setOnClickListener { if (post != null)  onClickListener.onAddFriendClicked(post)  }

        if (post != null)  holder.bind(post.postContent)
    }

    class FeedViewHolder(private val binding : ItemFeedBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(feed: PostContent) {
            binding.post = feed
            binding.executePendingBindings()
        }
    }

在设计选项卡中,一切看起来都很好:

但是,问题出现在真实设备中:

但是,如果我添加这样的视图,项目会覆盖整个屏幕:

 <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/transparent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/like_btn" />

任何帮助将不胜感激。提前致谢!!

【问题讨论】:

  • 你在android:background="@drawable/post_border"中设置宽高吗?
  • 嗨@saeedata,感谢您的回复。不,我没有在 post_border 中设置宽度和高度
  • 关于高度我 khonw 你的问题是 android:layout_height="wrap_content"
  • 你应该设置固定高度或设置 maxLines 但关于宽度我应该测试你的代码
  • 那么,如果我没记错的话,你建议我更改item_feed.xml的约束布局的layout_height?

标签: android android-studio android-layout kotlin android-recyclerview


【解决方案1】:

你的问题是

 android:layout_height="wrap_content"



<TextView
            android:id="@+id/post_content"
            style="@style/SubHeading"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:gravity="start"
            android:text="@{post.content}"
            android:textAlignment="viewStart"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/username"
            tools:text="Anything random ...." />

您必须设置 fixedHeight 或使用 ma​​xLines 并将宽度设置为 match_parent

同时删除这一行 android:textAlignment="viewStart"

【讨论】:

    【解决方案2】:

    我相信问题出在这里:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
        return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context)))
    }
    

    因为您正在调用未指定父视图的inflate() 的重载,所以您的项目视图的布局参数将被忽略。改为:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
        return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多