Вот основной макет, который состоит из макроса просмотра, сворачивания панели инструментов и другой компоновки, которую я использую
, поэтому установка высоты обертки просмотра рециркулятора не решила проблему, и я правильно передаю размер элемента в адаптере, также
надеюсь, вы поможете мне найти то, что я делаю неправильно, я уже несколько дней занимаюсь этим
основной макет
<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
}
}
Отдельный предмет, который я пытаюсь накачать в утилитаре
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>