Skip to content

Commit

Permalink
fix: use onClick
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <zhouxinyu1001@gmail.com>
  • Loading branch information
Zxilly committed Sep 7, 2021
1 parent 9960505 commit 01ed100
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package top.learningman.mipush

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_messages.*
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/top/learningman/mipush/utils/MessageAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package top.learningman.mipush.utils

import android.app.AlertDialog
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -13,22 +18,42 @@ import top.learningman.mipush.R
class MessageAdapter : ListAdapter<Message, MessageAdapter.MessageHolder>(WordsComparator()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageHolder {

return MessageHolder.create(parent)
}

override fun onBindViewHolder(holder: MessageHolder, position: Int) {
val current = getItem(position)
holder.bind(current)
holder.bind(current)

}

class MessageHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val messageItemTitleView: TextView = itemView.findViewById(R.id.row_item_title)
private val messageItemContentView: TextView = itemView.findViewById(R.id.row_item_content)
private val messageItemTimeView: TextView = itemView.findViewById(R.id.row_item_time)
private val messageItem: ConstraintLayout = itemView.findViewById(R.id.row_item)


fun bind(msg: Message?) {
messageItemTitleView.text = msg?.title
messageItemContentView.text = msg?.content

val relativeTime = msg?.time?.let { DateUtils.getRelativeTimeSpanString(it) }
messageItemTimeView.text = relativeTime

// val dialogView = LayoutInflater.from(itemView.context).inflate(R.layout.message_dialog,null,false)

messageItem.setOnClickListener {
Toast.makeText(
it.context,
"Clicked Item at ${this.adapterPosition}",
Toast.LENGTH_SHORT
).show()
AlertDialog.Builder((itemView.context))
.setTitle(msg?.title)
.create()
}
}

companion object {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/message_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialog_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialog_long"
android:visibility="gone"/>
</LinearLayout>
37 changes: 30 additions & 7 deletions app/src/main/res/layout/text_row_item.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_item"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:layout_margin="@dimen/margin_medium"
android:orientation="vertical">
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/margin_medium">

<TextView
android:id="@+id/row_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/msg_title" />
android:text="@string/loading"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp"
android:theme="@style/msg_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/row_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/msg_content" />
</LinearLayout>
android:layout_marginTop="5dp"
android:text="@string/loading"
android:textSize="14sp"
android:theme="@style/msg_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/row_item_title" />

<TextView
android:id="@+id/row_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/loading"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 0 additions & 4 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@
<style name="msg_title">
<item name="android:layout_marginBottom">8dp</item>
<item name="android:paddingLeft">8dp</item>

<item name="android:textAppearance">@android:style/TextAppearance.Material.Title</item>
</style>

<style name="msg_content">
<item name="android:layout_marginBottom">8dp</item>
<item name="android:paddingLeft">8dp</item>

<item name="android:textAppearance">@android:style/TextAppearance.Material.Menu</item>
</style>
</resources>

0 comments on commit 01ed100

Please sign in to comment.