RecyclerView不充气的物品

问题描述 投票:0回答:1

这里是由recyclerview,collapsingtoolbar布局和我正在使用的其他布局组成的主要布局

使recyclerview包装的高度变高并不能解决问题,而且我也在适配器中正确传递了物品的大小,

希望您能帮助我发现我在做错什么,我已经在这上待了几天

主布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/love_music"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TITILE"
                    android:textColor="@android:color/white"
                    android:textSize="20sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="subtitle"
                    android:textColor="@android:color/white"
                    android:textSize="18sp" />

            </LinearLayout>
        </RelativeLayout>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

<include layout="@layout/content_main" />

下面是我的适配器类

class FeedsAdapter(val items: List<Advert>) : RecyclerView.Adapter<FeedsAdapter.ViewHolder>()  {



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val v = LayoutInflater.from(parent.context).inflate(R.layout.item_card, parent,false)
    return ViewHolder(v)
}

override fun getItemCount(): Int = items.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    val post:Advert = items[position]

    holder.postName.text = post.username
    holder.postDesc.text = post.description
    holder.postLocation.text = post.location
    holder.timePosted.text = post.created_at
    Picasso.get().load(post.image1).into(holder.postImage)



}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val postName = itemView.findViewById(R.id.post_name) as TextView
    val postDesc = itemView.findViewById(R.id.post_description) as TextView
    val postLocation = itemView.findViewById(R.id.post_location)as TextView
    val timePosted = itemView.findViewById(R.id.post_time_posted) as TextView
    val postImage = itemView.findViewById(R.id.post_image) as ImageView

}

}

我试图调用的片段使用布局

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?

): View? {


    homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
    val root = inflater.inflate(R.layout.fragment_home, container, false)
    val recyclerView = view?.findViewById<RecyclerView>(R.id.advert_recyclerView)
    recyclerView?.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL ,false)
    //val textView: TextView = root.findViewById(R.id.text_home)
   /*   homeViewModel.text.observe(this, Observer {
        textView.text = it

    })*/



    Coroutines.main{
        ApiClient.instance.getAdvert().enqueue(object: Callback<AdvertResponse> {
            override fun onFailure(call: Call<AdvertResponse>, t: Throwable) {

                Toast.makeText(activity,"login failed ${t.message}",Toast.LENGTH_LONG).show()

            }

            override fun onResponse(
                call: Call<AdvertResponse>,
                response: Response<AdvertResponse>
            ) {

                val ads: List<Advert> = response.body()!!.Advert
                val adapter = FeedsAdapter(ads)

                recyclerView?.adapter = adapter

                adapter.notifyDataSetChanged()

            }

        })
    }
    return root

}

}

我正在尝试将其充气到recyclerview中的单个项目

androidx.cardview.widget.CardView 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">


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

    <ImageView
        android:id="@+id/post_profile_pic"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/post_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="Ruth Agwu"
        app:layout_constraintStart_toEndOf="@+id/post_profile_pic"
        app:layout_constraintTop_toTopOf="@+id/post_profile_pic" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="45dp"
        android:layout_height="12dp"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toEndOf="@+id/post_name"
        app:layout_constraintTop_toTopOf="@+id/post_name" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/ratingBar"
        app:srcCompat="@drawable/more_vert_24dp" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toEndOf="@+id/post_profile_pic"
        app:layout_constraintTop_toBottomOf="@+id/post_name"
        app:srcCompat="@drawable/location_on_24dp" />

    <TextView
        android:id="@+id/post_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Jabi"
        app:layout_constraintStart_toEndOf="@+id/imageView8"
        app:layout_constraintTop_toBottomOf="@+id/post_name" />

    <TextView
        android:id="@+id/post_time_posted"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="25 min ago"
        app:layout_constraintStart_toEndOf="@+id/post_location"
        app:layout_constraintTop_toTopOf="@+id/post_location" />

    <ImageView
        android:id="@+id/post_image"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_time_posted"
        app:srcCompat="@drawable/solar_panel" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@drawable/ash_background"
        app:layout_constraintBottom_toBottomOf="@+id/post_image"
        app:layout_constraintEnd_toEndOf="@+id/post_image"
        app:layout_constraintStart_toStartOf="@+id/post_image">

        <TextView
            android:id="@+id/post_description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:gravity="start|top"
            android:text="We specialize on all kinds of cakes and snack's. cooking Catering and events management"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

android android-recyclerview recycler-adapter
1个回答
0
投票

将家庭片段更改为此就像魔术一样工作>>

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel

lateinit var rvRecyclerView : RecyclerView


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?


): View? {
    var view = inflater.inflate(R.layout.fragment_home, container, false)

    rvRecyclerView = view!!.findViewById<RecyclerView>(R.id.advert_recyclerView)
    rvRecyclerView.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL ,false)

    //homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)



    fetchFeeds()


    return view

}


private fun fetchFeeds() {

    Coroutines.main{
        ApiClient.instance.getAdvert().enqueue(object: Callback<AdvertResponse> {
            override fun onFailure(call: Call<AdvertResponse>, t: Throwable) {

                Toast.makeText(activity,"login failed ${t.message}",Toast.LENGTH_LONG).show()

            }

            override fun onResponse(
                call: Call<AdvertResponse>,
                response: Response<AdvertResponse>
            ) {

                val ads: List<Advert> = response.body()!!.Advert
                val adapter = FeedsAdapter(ads)

                rvRecyclerView.adapter = adapter

                adapter.notifyDataSetChanged()

            }

        })
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.