来自一枚敢敢的Kotlin——Android开发日志之初识RecyclerView

写在前面

​ 因为本科课程的原因,我在2022年开始了我的android开发,我以为我跳离了前端,其实没有,我只是从web端换到了移动端,如此美妙的开局!学校里面的内容还在用Java上着android,我感觉Java写android就像在写jsp一样,在了解到Kotlin下写android可能会更轻松之后,我选择使用Kotlin写,可能是开发团队里面有之前大佬留下来的代码才让我再即将面临考研危机之前也敢触碰一个陌生的语言,但是从编码的风格来说其实挺像Typescript的,只是不同于web前端的是,我需要处理更多关于线程和本地资源的问题。

​ 希望通过我的开发日志,记录下我写这个系统——项目管理系统app的回忆,也方便我日后重拾起这一技术提供一个方便。

RecycleView

这是我一上来遇到的最大的拦路虎,因为之前上课的时候老师误以为我们都是张无忌,妄图三招教会我们太极😀,结果就是我神游太虚。于是乎无招胜有招,开始了我Kotlin编码生涯下的android世界。我首先要完成的就是项目管理系统中的重中之重——考勤,于是乎我需要自主搭建一个日历,通过日历控件去描述反应,一个人的打卡情况,一天是否签到,一天的在线时长这些都需要我通过这一控件去呈现,因为是初学,所以部分原型借鉴了其他大佬。

日历UI——XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="33dp"
        android:orientation="horizontal"
        android:paddingRight="15dp"
        android:paddingLeft="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text=""
            />

    </LinearLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sign"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="15dp"
        android:paddingLeft="15dp"
        tools:itemCount="35"
        tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        tools:listitem="@layout/sign_item_activity"
        tools:spanCount="7"/>
    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:text="签到"/>
</LinearLayout>

效果如下

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#F5F5F5"
    android:layout_height="60dp"
    tools:ignore="MissingDefaultResource"
    tools:layout_width="47dp">

    <TextView
        android:id="@+id/sign_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="1"
        android:textColor="#434343"
        android:textSize="18dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/sign_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="已签"
        android:textColor="#999999"
        android:textSize="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/sign_date" />

    <TextView
        android:id="@+id/sign_number"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:background="#0B7BFD"
        android:gravity="center"
        android:text="5"
        android:textColor="#FFFFFF"
        android:textSize="7dp"
        app:layout_constraintCircle="@+id/sign_date"
        app:layout_constraintCircleAngle="45"
        app:layout_constraintCircleRadius="15dp"
        app:layout_constraintLeft_toRightOf="@id/sign_date"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="1dp"
        android:layout_height="0dp"/>

    <View
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="#FFFFFF"
        android:layout_width="0dp"
        android:layout_height="1dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>

效果如下:

在这里插入图片描述

Adapter

SignIn and Adapter

data class SignIn (val sign_date : String , val sign_number : String, val sign_state : String?) : Serializable
class SignInAdapter(val SingInList: List<SingIn>): RecyclerView.Adapter<SignInAdapter.ViewHolder>(){
   
    inner class ViewHolder (view: View):RecyclerView.ViewHolder(view) {
        val sign_date = view.findViewById<TextView>(R.id.sign_date)
        val sign_number = view.findViewById<TextView>(R.id.sign_number)
        val sign_state = view.findViewById<TextView>(R.id.sign_state)
   }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = SingInList[position]
        println(SingInList[position].toString())
        holder.sign_date.text = item.sign_date
        holder.sign_number.text = item.sign_number
        holder.sign_state.text = item.sign_state
        
    }

    override fun getItemCount(): Int {
        return SingInList.size;
    }
}

在编写适配器的时候出现了一个极其经典的bug——java.lang.NullpotinerException : Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference,这个bug困扰了相当长的一个时间,最后发现android的xml命名格式不支持驼峰命名法,只能奖之前写在一个短语里面的拆分。然后就解决了

在这里插入图片描述

SignActivity

class SignInActivity: BaseActivity() {
    final var logTag = LogTag("SigInActivity")

    lateinit var adapter : SignInAdapter

    var list = ArrayList<SingIn>()

    private lateinit var inflater : LayoutInflater

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.signin_main_activity)
        initDate();
        sign.layoutManager = GridLayoutManager(this,7)
        adapter = SignInAdapter(list)
        sign.adapter = adapter
    }

    private fun initDate(){
        var index : Int = 1;
        while (true){
            if (list.size == 35){
                break
            }
            list.add(SingIn("1","5","已签"))
            index++;
        }
    }
}

以上就是我的第一个小task或者说target的记录情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecyclerView is a UI component that is used to display large sets of data in an efficient and scrollable manner. The RecyclerView widget is a more advanced and flexible version of ListView. To use RecyclerView, you need to create an adapter that will hold the data and create the views to be displayed. Here is an example of a RecyclerView adapter in Kotlin: ``` class MyAdapter(private val dataList: List<MyData>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val data = dataList[position] holder.bind(data) } override fun getItemCount() = dataList.size inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(data: MyData) { // bind data to views itemView.textViewTitle.text = data.title itemView.textViewDescription.text = data.description } } } ``` In this example, `MyData` is a data class that holds the data to be displayed in the RecyclerView. The `MyAdapter` class takes a list of `MyData` objects as a parameter in the constructor. The `onCreateViewHolder` method inflates a layout file (`item_layout.xml` in this case) and returns a `ViewHolder` object. The `onBindViewHolder` method binds the data to the views in the `ViewHolder`. Finally, the `getItemCount` method returns the number of items in the list, which is the size of the `dataList`. The `ViewHolder` class holds the views that will be displayed in the RecyclerView and the `bind` method binds the data to the views.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值