Kotlin Android 第20天,从 0 到 ML - RecyclerView - GradView

前言:

  RecyclerView 的 ListView 完成了,但我想要用GradView怎麽辨?
  超简单的,昨天的item 和 adapter 都不用改,直接改activity就ok了,
  让我们看下去(有看过Day19的文章就直接看Activity)。

大纲 :

RecyclerView 的步骤

build.gradle(app)

dependencies {
 implementation "androidx.recyclerview:recyclerview:1.2.1"
}
adapter and view holder

class Day19Adapter(private val dataSet: List<String>) :  
                      RecyclerView.Adapter<Day19Adapter.ViewHolder>() {

/**
 * Provide a reference to the type of views that you are using
 * (custom ViewHolder).
 */
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val dataView: TextView
    init {
        dataView = view.findViewById(R.id.textView4)
    }

}

// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    // Create a new view, which defines the UI of the list item
val v = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)

    return ViewHolder(v)
}

// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Get element from your dataset at this position and replace the
// contents of the view with that element
holder.dataView.text = dataSet[position]
holder.itemView.setOnClickListener { 
    Toast.makeText(it.context, "第 $position 项被按下", Toast.LENGTH_SHORT).show() }
}

// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount(): Int {
    return dataSet.size
 }
}

item.xml

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

<ImageView
    android:id="@+id/imgV"
    android:layout_width="40dp"
    android:layout_height="40dp"
    app:srcCompat="@drawable/logo"
    android:layout_margin="5dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_gravity="center"/>

<TextView
    android:id="@+id/textView4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_marginStart="16dp"
    android:layout_toEndOf="@+id/imgV"
    android:text="GDG Taipei"
    android:gravity="center_vertical"
    android:textSize="24sp"/>
 </RelativeLayout>    

https://ithelp.ithome.com.tw/upload/images/20210924/20121643cjgOtwtIye.png

Activity:

val listData = ArrayList<String>()
    for (i in 0..41) {
        when(i % 7){
            0 -> listData.add("GDG 台北")
            1 -> listData.add("GDG 桃园")
            2 -> listData.add("GDG 新竹")
            3 -> listData.add("GDG 台中")
            4 -> listData.add("GDG 台南")
            5 -> listData.add("GDG 高雄")
            6 -> listData.add("GDG 花莲")
        }
    }
    
    
    val layoutManager = GridLayoutManager(this,3)
    val dataList = findViewById<RecyclerView>(R.id.recyclerView)
    dataList.layoutManager = layoutManager
    dataList.adapter = Day19Adapter(listData)
    

adapter还是用day19的都不变
layoutManager 原本是 LinearLayoutManager(this) 改成
layoutManager = GridLayoutManager(this,2)

设定GridLayoutManager填入 spanCount 数
spanCount 数要分为几个项目作显示。


执行2列和3列结果:
https://ithelp.ithome.com.tw/upload/images/20210925/20121643Qw4CYBO7Hp.png
https://ithelp.ithome.com.tw/upload/images/20210925/20121643OExKXt5qnm.png

参考:

https://developer.android.com/guide/topics/ui/layout/recyclerview


<<:  Day11:今天我们来聊一下Parrot Security上的Enum4linux

>>:  Powershell 入门之函数

ZFS优化与修复

光是建立ZFS pool是不够的,如果要能使用rsync来定期备份unRaid Array,就必须对...

【Day28】建立一个 LUIS Bot

今天我们要来将 Chatbot 与 Language Understanding Service (...

Day09 | Dart 非同步 - Future

昨天介绍了在Dart中非同步的基本概念,今天就要来讲到如何简单的控制非同步操作。 Future Fu...

距离感测模组

在网路上面google距离感测 会发现有很多方式的模组可供选择 有简单的超音波 也有红外线 甚至是雷...

认识 C# 的 存取层级修饰词

修饰词~可以限制类别的存取层级 我的举例是:像是一些私密的东西你不想让别人随便乱看一样 就要设隐私权...