From a daring Kotlin -- the first knowledge of Android development log RecyclerView

Write in front

Because of the undergraduate course, I started my android development in 2022. I thought I jumped away from the front end, but I didn't. I just changed from the web end to the mobile end. It's such a wonderful start! The content in the school is still using java to write Android. I feel that writing Android in Java is like writing jsp. After learning that it may be easier to write Android in Kotlin, I chose to write it in Kotlin. It may be that the code left by the previous leaders in the development team makes me dare to touch a strange language before I face the crisis of postgraduate entrance examination. However, from the perspective of coding style, it is quite like Typescript, Just different from the web front end, I need to deal with more problems about threads and local resources.

I hope that through my development log, I can record my memory of writing this system - project management system app, which will also facilitate me to pick up this technology in the future.

RecycleView

This is the biggest stumbling block I encountered when I came up, because the teacher mistakenly thought we were all Zhang Wuji in class and tried to teach us Tai Chi with three moves 😀, The result is that I wander too empty. So I started the android world in my Kotlin coding career. The first thing I need to finish is the top priority in the project management system - attendance. Therefore, I need to build a calendar independently, and use the calendar control to describe the reaction, a person's clock in situation, whether to check in one day, and the online time of one day. All these need to be presented through this control. Because it is a beginner, some prototypes learn from other big guys.

Calendar 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="day" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="one"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="two"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="three"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="four"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="five"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#999999"
            android:gravity="center"
            android:text="six"
            />

    </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="Sign in"/>
</LinearLayout>

The effect is as follows

<?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="Signed"
        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>

The effect is as follows:

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;
    }
}

When writing the adapter, there was an extremely classic bug - Java lang.NullpotinerException : Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence) 'on a null object reference. This bug has been bothered for quite a long time. Finally, it is found that Android's xml naming format does not support hump naming, and can only award the split written in a phrase before. Then it was solved

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","Signed"))
            index++;
        }
    }
}

The above is the record of my first small task or target.

Tags: Android kotlin

Posted by bobby4 on Mon, 18 Apr 2022 15:50:00 +0930