Skip to content

Commit

Permalink
Optimise UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Uynaity committed Sep 18, 2023
1 parent a6cf84f commit 134c807
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 26 deletions.
30 changes: 29 additions & 1 deletion app/src/main/java/com/uynaity/ezcc2/ListAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
package com.uynaity.ezcc2

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class SpaceItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
outRect.top = space
outRect.bottom = space
}
}

class ListAdapter : RecyclerView.Adapter<ListAdapter.ViewHolder>() {
private var data: MutableList<String> = mutableListOf()
private var listener: OnItemClickListener? = null

fun setDataList(item: MutableList<String>) {
data = item
notifyDataSetChanged()
}

fun setOnItemClickListener(listener: OnItemClickListener) {
this.listener = listener
}

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.textView.text = item

holder.itemView.setOnClickListener {
listener?.onItemClick(item)
}
}

override fun getItemCount(): Int {
Expand All @@ -31,4 +55,8 @@ class ListAdapter : RecyclerView.Adapter<ListAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.text_view)
}

interface OnItemClickListener {
fun onItemClick(item: String)
}
}
29 changes: 22 additions & 7 deletions app/src/main/java/com/uynaity/ezcc2/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.VibrationEffect
import android.os.Vibrator
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
Expand Down Expand Up @@ -87,6 +88,7 @@ class MainActivity : AppCompatActivity() {
adapter1 = ListAdapter()
adapter2 = ListAdapter()


val spinner = findViewById<Spinner>(R.id.spinner)
val adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_options, android.R.layout.simple_spinner_item
Expand Down Expand Up @@ -114,7 +116,7 @@ class MainActivity : AppCompatActivity() {

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?, view: android.view.View?, position: Int, id: Long
parent: AdapterView<*>?, view: View?, position: Int, id: Long
) {
while (semList[0].isEmpty() || semList[1].isEmpty()) {
Thread.sleep(100)
Expand Down Expand Up @@ -158,6 +160,18 @@ class MainActivity : AppCompatActivity() {
judgement()
printSem()
}

adapter1.setOnItemClickListener(object : ListAdapter.OnItemClickListener {
override fun onItemClick(item: String) {
Toast.makeText(this@MainActivity, "功能开发中...", Toast.LENGTH_SHORT).show()
}
})

adapter2.setOnItemClickListener(object : ListAdapter.OnItemClickListener {
override fun onItemClick(item: String) {
Toast.makeText(this@MainActivity, "功能开发中...", Toast.LENGTH_SHORT).show()
}
})
}

@Deprecated("Deprecated in Java")
Expand All @@ -180,7 +194,8 @@ class MainActivity : AppCompatActivity() {
}
} catch (e: Exception) {
runOnUiThread {
Toast.makeText(this, "更新失败,请检查网络链接,程序退出", Toast.LENGTH_LONG).show()
Toast.makeText(this, "更新失败,请检查网络链接,程序退出", Toast.LENGTH_LONG)
.show()
}
finish()
}
Expand Down Expand Up @@ -232,11 +247,11 @@ class MainActivity : AppCompatActivity() {
var stat = ""

if (data2 > data3) {
stat = "尚有余,可选\n"
stat = "尚有余,可选"
} else if (data3 - data2 <= 3) {
stat = "少量不足,可备选\n"
stat = "少量不足,可备选"
} else if (data3 - data2 > 3) {
stat = "严重不足,建议更换\n"
stat = "严重不足,建议更换"
}
val dataSet = mapOf(
"课程代码" to data1,
Expand Down Expand Up @@ -282,7 +297,7 @@ class MainActivity : AppCompatActivity() {
val spinner = findViewById<Spinner>(R.id.spinner)
val selectedValue = spinner.selectedItem.toString()
val c = courseCodeEditText.text.toString()
val mergeC = "$selectedValue$c"
val mergeC = "CC${selectedValue}${c}"
if (!i["课程代码"].toString().contains(mergeC)) {
continue
}
Expand All @@ -307,7 +322,7 @@ class MainActivity : AppCompatActivity() {
if (caches[index].isEmpty()) {
stats[index] = "未在Sem ${index + 1}中找到相关课程 :-("
} else {
stats[index] = "Sem ${index + 1}中找到以下相关课程:"
stats[index] = "Sem ${index + 1}中找到以下课程:"
}
}
sem1Stat.text = stats[0]
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/ripple_effect.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/rippleColor">

<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>

<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>

</ripple>
19 changes: 11 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@
android:hint="@string/course_code"
android:inputType="number"
android:maxLength="4"
android:minHeight="48dp" />
android:minHeight="48dp"
android:textSize="14sp" />

<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_height="match_parent"
android:contentDescription="@string/refresh"
android:text="刷新" />
android:text="刷新"
android:textSize="14sp" />
</LinearLayout>

<LinearLayout
Expand All @@ -160,21 +160,24 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="@string/noOfEmpty" />
android:text="@string/noOfEmpty"
android:textSize="12sp" />

<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="@string/exceptX" />
android:text="@string/exceptX"
android:textSize="12sp" />

<Switch
android:id="@+id/switch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="@string/viewAvailable" />
android:text="@string/viewAvailable"
android:textSize="12sp" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
20 changes: 14 additions & 6 deletions app/src/main/res/layout/list_item_layout.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_marginVertical="6dp">

<TextView
android:id="@+id/text_view"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:orientation="vertical">

</LinearLayout>
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect" />

</LinearLayout>

</androidx.cardview.widget.CardView>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@
<color name="md_theme_dark_surfaceTint">#8ADB4C</color>
<color name="md_theme_dark_outlineVariant">#44483E</color>
<color name="md_theme_dark_scrim">#000000</color>
<color name="rippleColor">#34558B2F</color>
</resources>
8 changes: 4 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<string name="viewAvailable">仅看有余</string>

<string-array name="spinner_options">
<item>CCST</item>
<item>CCHU</item>
<item>CCGL</item>
<item>CCCH</item>
<item>ST</item>
<item>HU</item>
<item>GL</item>
<item>CH</item>
</string-array>
</resources>

0 comments on commit 134c807

Please sign in to comment.