我想添加水平图像滚动条,如下面附加的图像。我知道有解决方案,但所有解决方案都有 4 到 5 年的历史,在 Kotlin 和约束布局之后,我假设现在有一些好的方法可以实现这一目标。请帮助我并分享最好、最简单的方法来做到这一点。提前致谢。
最佳答案
创建项目:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@color/colorAccent"
card_view:cardCornerRadius="8dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher_round" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="@string/app_name"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
然后将此项目添加到您的RecyclerView
中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" />
</LinearLayout>
然后从您的 Activity 中调用它,并为其设置一个 HORIZONTAL
LayoutManager 及其适配器:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
displayList()
}
private fun displayList() {
val imageList = ArrayList<ImageDataModel>()
imageList.clear()
imageList.add(ImageDataModel("https://conversionxl.com/wp-content/uploads/2018/09/coding-language.jpg", "Test"))
imageList.add(ImageDataModel("https://makeawebsitehub.com/wp-content/uploads/2016/02/learn-code-e1455713167295.jpg", "Test"))
imageList.add(ImageDataModel("https://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png", "Test"))
imageList.add(ImageDataModel("https://conversionxl.com/wp-content/uploads/2018/09/coding-language.jpg", "Test"))
imageList.add(ImageDataModel("https://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png", "Test"))
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.HORIZONTAL, false)
recyclerView.adapter = ViewAdapter(imageList)
}
}
正如您所看到的,RecyclerView 也需要一个适配器,它是一个简单的适配器,使用 Glide显示这样的图像:
class ViewAdapter(private val imageDataModelList: ArrayList<ImageDataModel>) : RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(imageDataModelList[position])
}
override fun getItemCount(): Int {
return imageDataModelList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(imageDataModel: ImageDataModel) {
val textView = itemView.findViewById<TextView>(R.id.tvName)
val imageView = itemView.findViewById<ImageView>(R.id.imageView)
textView.text = imageDataModel.name
Glide.with(itemView.context).load(imageDataModel.url).into(imageView)
}
}
}
不要忘记在应用程序级 build.gradle
中添加 Glide 的依赖项:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}
该应用程序应如下所示: /image/fpGuq.jpg
关于安卓 : clickable Image horizontal slider using kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199370/