我是 Kotlin 和 Android Studio 的初学者,我想请求帮助来解决这个问题:
` 私人乐趣 downloadImage(item: Item) = Thread {
val imageConnection = URL(imageItem.url).openConnection() as HttpURLConnection
try {
if (photosConnection.responseCode == HTTP_OK) {
runOnUiThread {
val bitmap = BitmapFactory.decodeStream(photosConnection.inputStream)
activityMainBinding.objImageView.apply {
// Implementar o novo Adapter
}
}
} else {
runOnUiThread{
Toast.makeText(this, " Deu erro !!!", Toast.LENGTH_SHORT).show()
}
}
} catch ( e: Exception) {
e.printStackTrace()
} finally {
imageConnection.disconnect()
}
}.start()`
我尝试制作这个适配器,这是我作为参考。但是继承自ArrayAdapter我知道与Bitmap类型不兼容。我也不确定通常我使用的对象(android.R.layout)(android.R.layout.simple_list_item_1)当它们是字符串列表应用程序时。但在这种情况下,我没有找到类似的,例如(android.R.layout.image)。
` 类 ImageAdapter ( private val ActivityContext:上下文, 私有 val imageList:MutableList ): ArrayAdapter(activityContext, android.R.layout.simple_list_item_1, photosList) {
private data class ImageHolder( val imageVw: ImageView )
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val imageView = convertView ?: LayoutInflater.from(activityContext)
.inflate(android.R.layout.simple_list_item_1,parent,false).apply {
tag = ImageHolder(findViewById(android.R.id.text1))
}
(imageView.tag as ImageHolder)imageVw.setImageBitmap = imageList[position]
return imageView
}
}`
我感谢任何帮助
我会尽量保持简单
使用库下载图像,例如glide,它非常简单,可以处理所有其他事情,例如 url 连接、线程和缓存图像。
为此,您应该使用
RecyclerView
来创建 RecyclerView.Adapter
的子类。
还可以使用 glide 库完成。
现在的代码,这是 Activity 布局,您必须将 recyclerView 放在某处以显示带有图像的列表,在我的示例中
activity_image_adapter.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".simpleimageAdapter.ImageAdapterActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="4"
tools:listitem="@layout/item_image"/>
</FrameLayout>
还为图像项目创建布局,在我的示例中
item_image.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemImageView"
android:layout_width="match_parent"
android:layout_height="256dp"
android:importantForAccessibility="no"
android:scaleType="centerCrop"/>
下一步您需要为 recyclerView 创建自定义适配器:
class ImageAdapter : RecyclerView.Adapter<ImageAdapter.ImageViewHolder>() {
//Data list for the adapter, we will set data later
private var images: List<ImageItem> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
//Creates view holder which holds view. Recycler is reusing views because of performance
val inflater = LayoutInflater.from(parent.context)
//Create view and attach it to the parent
val view = inflater.inflate(R.layout.item_image, parent, false)
return ImageViewHolder(view)
}
override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
//Called to show data with holder
holder.onBind(item = images[position])
}
override fun getItemCount(): Int {
//Returns items count this adapter shows
return images.size
}
fun setData(list: List<ImageItem>) {
this.images = list
//Set new dataset for adapter and notify it about change
//notifyDataSetChanged isn't the best option but for now let's keep it simple
notifyDataSetChanged()
}
inner class ImageViewHolder(contentView: View) : RecyclerView.ViewHolder(contentView) {
private val imageView: ImageView = contentView.findViewById(R.id.itemImageView)
fun onBind(item: ImageItem) {
//Load image with glide library and show it with imageView
Glide.with(imageView.context)
.load(item.url)
.centerCrop()
.into(imageView)
}
}
}
现在将它们全部放在活动代码中:
class ImageAdapterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_adapter)
//Just simple data for the example
val data = listOf(
ImageItem(url = "https://static.vecteezy.com/system/resources/thumbnails/025/284/015/small_2x/close-up-growing-beautiful-forest-in-glass-ball-and-flying-butterflies-in-nature-outdoors-spring-season-concept-generative-ai-photo.jpg"),
ImageItem(url = "https://i0.wp.com/picjumbo.com/wp-content/uploads/beautiful-nature-mountain-scenery-with-flowers-free-photo.jpg?w=600&quality=80"),
ImageItem(url = "https://media.istockphoto.com/id/517188688/photo/mountain-landscape.jpg?s=612x612&w=0&k=20&c=A63koPKaCyIwQWOTFBRWXj_PwCrR4cEoOw2S9Q7yVl8=")
)
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
val imageAdapter = ImageAdapter()
//Set adapter to the recycler
recyclerView.adapter = imageAdapter
//Set data to adapter
imageAdapter.setData(list = data)
}
}