android 回收器视图未显示从API正确提取的图像

6gpjuf90  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(123)

我正在使用Pixabay API获取图像,我得到了想要的结果,但有一个问题,请参见下面的视频。
https://drive.google.com/file/d/1TznwxEgPtryl-HXdfl8DkzkCTLiG6Ohp/view?usp=share_link
我提供了代码片段
ImageAdapter.kt

private val items = mutableListOf<Hit>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.image_items,parent,false)
        return ImageViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val currentItem = items[position]
        holder.image_id.text = currentItem.id.toString()
        Glide.with(holder.itemView.context).load(currentItem.largeImageURL).into(holder.image)
    }

    override fun getItemCount(): Int {
        return items.size

    }

    fun initData(itemsList:List<Hit>){
        this.items.clear()
        this.items.addAll(itemsList)
        notifyDataSetChanged()
    }

    class ImageViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        val image = itemView.findViewById<ImageView>(R.id.tv_Image)
        val image_id = itemView.findViewById<TextView>(R.id.image_id)
    }

}

图像视图模型

init {
        GlobalScope.launch {
            repo.getImages()
        }
    }

    val images:LiveData<List<Hit>>
    get() = repo. Images

}

主要活动

private lateinit var imageViewModel: ImageViewModel
    private lateinit var imageRecycler:RecyclerView
    private lateinit var adapter: ImageAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        imageRecycler  = findViewById(R.id.recyclerView)
        adapter = ImageAdapter()
        imageRecycler.layoutManager = LinearLayoutManager(this)
        imageRecycler.adapter = adapter

        val api  = RetrofitHelper.getInstance().create(PexelApi::class.java)
        val repo = ImageRepository(api)
        imageViewModel = ViewModelProvider(this,ImageViewModelFactory(repo))[ImageViewModel::class.java]
        imageViewModel.images.observe(this){
            list->
            adapter.initData(list)
        }

    }
}

activity_main.xml

<LinearLayout 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="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/image_items"
        />

</LinearLayout>

image_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.cardview.widget.CardView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_margin="10dp"
        android:elevation="10dp"
        app:cardCornerRadius="10dp"
        >
        <TextView
            android:id="@+id/image_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text="id"
            />
        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/tv_Image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            />
    </androidx.cardview.widget.CardView>

</LinearLayout>

我不知道怎么解决这个问题,可能是模型有问题

7d7tgy0s

7d7tgy0s1#

您正在将每个项目的高度设置为match_parent

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

只要把它改成wrap_content就可以了。

相关问题