Android 回收查看不膨胀的物品
这里是由recyclerview、CollasingToolbar布局和我正在使用的其他布局组成的主布局 制作recyclerview包装的高度并没有解决问题,我也正确地将物品的大小传递到适配器中 希望你能帮我找出我做错了什么,我已经做了好几天了 主布局Android 回收查看不膨胀的物品,android,android-recyclerview,Android,Android Recyclerview,这里是由recyclerview、CollasingToolbar布局和我正在使用的其他布局组成的主布局 制作recyclerview包装的高度并没有解决问题,我也正确地将物品的大小传递到适配器中 希望你能帮我找出我做错了什么,我已经做了好几天了 主布局 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a
<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
}
类FeedsAdapter(val项:列表):RecyclerView.Adapter(){
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val v=LayoutFlater.from(父级上下文)。充气(R.layout.item\u卡,父级,false)
返回视窗支架(v)
}
重写getItemCount():Int=items.size
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val post:广告=项目[位置]
holder.postName.text=post.username
holder.postDesc.text=post.description
holder.postLocation.text=post.location
holder.timePosted.text=post.created\u在
毕加索.get().load(post.image1).into(holder.postImage)
}
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val postName=itemView.findviewbyd(R.id.post_name)作为TextView
val postDesc=itemView.findviewbyd(R.id.post_description)作为文本视图
val postLocation=itemView.findViewById(R.id.post_位置)作为TextView
val timePosted=itemView.findviewbyd(R.id.post\u time\u posted)作为文本视图
val postImage=itemView.findviewbyd(R.id.post_image)作为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
}
class HomeFragment:Fragment(){
私有lateinit var homeViewModel:homeViewModel
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
homeViewModel=ViewModelProviders.of(this.get)(homeViewModel::class.java)
val root=充气机。充气(R.layout.fragment_home,container,false)
val recyclerView=view?.findviewbyd(R.id.advert\u recyclerView)
recyclerView?.layoutManager=LinearLayoutManager(活动,LinearLayoutManager.VERTICAL,false)
//val textView:textView=root.findViewById(R.id.text\u home)
/*homeViewModel.text.observe(此,观察者{
textView.text=it
})*/
共程{
ApiClient.instance.getAdvert().enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Toast.makeText(活动,“登录失败${t.message}”,Toast.LENGTH\u LONG.show()
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
val ads:List=response.body()!!.Advert
val适配器=馈线适配器(ads)
recyclerView?.adapter=适配器
adapter.notifyDataSetChanged()
}
})
}
返回根
}
}
我试图将单个物品充气到回收视图中
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>
androidx.cardwiew.widget.cardwiew 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\u width=“match\u parent”android:layout\u height=“wrap\u content”>
将主片段更改为此效果非常神奇
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()
}
})
}
}
class HomeFragment:Fragment(){
私有lateinit var homeViewModel:homeViewModel
lateinit变量rvRecyclerView:RecyclerView
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
var视图=充气机。充气(R.layout.fragment\u home,container,false)
rvRecyclerView=视图!!.findViewById(R.id.advert\u recyclerView)
rvRecyclerView.layoutManager=LinearLayoutManager(活动,LinearLayoutManager.VERTICAL,false)
//homeViewModel=ViewModelProviders.of(this.get)(homeViewModel::class.java)
fetchFeeds()
返回视图
}
私人娱乐fetchFeeds(){
共程{
ApiClient.instance.getAdvert().enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Toast.makeText(活动,“登录失败${t.message}”,Toast.LENGTH\u LONG.show()
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
val ads:List=response.body()!!.Advert
val适配器=馈线适配器(ads)
rvRecyclerView.adapter=适配器
adapter.notifyDataSetChanged()
}
})
}
}
}