Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.4'
}
45 changes: 45 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package otus.gpb.recyclerview

import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil

class ChatAdapter : ListAdapter<ChatItem, ChatViewHolder> (DiffUtilItem()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatViewHolder {
return ChatViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.chat_item, parent, false)
)
}

override fun onBindViewHolder(viewHolder: ChatViewHolder, position: Int) {
viewHolder.bind(currentList[position])
}

fun removeItem(position: Int){
val list = currentList.toMutableList()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно добавьте проверку валидности позиции перед удалением

list.removeAt(position)
submitList(list)
notifyDataSetChanged()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListAdapter автоматически обновляет список через DiffUtil при вызове submitList, поэтому notifyDataSetChanged здесь не нужен и может вызвать лишние перерисовки. Можно убрать из методов removeItem и addItems.

}

fun addItems(count: Int) {
val list = currentList.toMutableList()
list.addAll(ChatDataGenerator.generateChatItemsList(count))
submitList(list)
notifyDataSetChanged()
}

class DiffUtilItem : DiffUtil.ItemCallback<ChatItem>() {

override fun areItemsTheSame(oldItem: ChatItem, newItem: ChatItem): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: ChatItem, newItem: ChatItem): Boolean {
return oldItem == newItem
}
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatDataGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package otus.gpb.recyclerview

object ChatDataGenerator {

private var lastId = 0
fun generateChatItemsList(count: Int): MutableList<ChatItem> {
return MutableList(count) {
ChatItem(
id = lastId++,
username = listOf(
"Anton Karavaev",
"MyWife",
"George",
"AK-47",
"Chernomor",
"Cat",
"Donald Trump",
"Paul McCartney",
"John Lennon",
"Eddie Van Halen"
).random(),
message = listOf(
"Hello",
"Good day",
"Здоровеньки булы...",
"How're you?",
"Шалом алейхем!",
"Хай, пипл!"
).random(),
userImage = listOf(
R.drawable.avatar_1,
R.drawable.avatar_2,
R.drawable.avatar_3,
R.drawable.avatar_4,
R.drawable.avatar_5,
R.drawable.avatar_6,
R.drawable.avatar_7,
R.drawable.avatar_8,
R.drawable.avatar_9,
R.drawable.avatar_10
).random(),
date = listOf(
"14:28",
"13:45",
"17:20",
"21:57",
"18:12"
).random()
)
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package otus.gpb.recyclerview

data class ChatItem(
val id: Int,
val userImage: Int,
val date: String,
val username: String,
val message: String
)
21 changes: 21 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ChatViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package otus.gpb.recyclerview

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.imageview.ShapeableImageView

class ChatViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

private val userImage = view.findViewById<ShapeableImageView>(R.id.user_avatar)
private val username = view.findViewById<TextView>(R.id.username)
private val message = view.findViewById<TextView>(R.id.message)
private val date = view.findViewById<TextView>(R.id.published_date)

fun bind(item: ChatItem) {
userImage.setImageResource(item.userImage)
username.text = item.username
message.text = item.message
date.text = item.date
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/ItemTouchHelperCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package otus.gpb.recyclerview

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class ItemTouchHelperCallback() : ItemTouchHelper.Callback() {

override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int = makeMovementFlags(0, ItemTouchHelper.LEFT)

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean = false

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
viewHolder.chatAdapter.removeItem(viewHolder.bindingAdapterPosition)
}

override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
return 0.50f
}

private val RecyclerView.ViewHolder.chatAdapter: ChatAdapter
get() = bindingAdapter as ChatAdapter

}
42 changes: 42 additions & 0 deletions app/src/main/java/otus/gpb/recyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,53 @@ package otus.gpb.recyclerview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var list: MutableList<ChatItem>
private val adapter by lazy { ChatAdapter() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)

val layoutManager = recyclerView.layoutManager as LinearLayoutManager

val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
divider.setDrawable(
ContextCompat.getDrawable(this, R.drawable.divider)
?: error("Not divider drawable")
)

recyclerView.addItemDecoration(divider)

ItemTouchHelper(ItemTouchHelperCallback()).attachToRecyclerView(recyclerView)

list = ChatDataGenerator.generateChatItemsList(20)

recyclerView.adapter = adapter

adapter.submitList(list)

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

val totalItemCount = layoutManager.itemCount
val lastVisibleItem = layoutManager.findLastVisibleItemPosition()

if (lastVisibleItem + 1 == totalItemCount) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

При быстром скролле пагинация может срабатывать несколько раз подряд, что приводит к множественным одновременным загрузкам данных. Можно использовать флаг isLoading, чтобы предотвратить повторные вызовы addItems до завершения текущей загрузки

adapter.addItems(5)
}
}
})
}
}
40 changes: 40 additions & 0 deletions app/src/main/res/drawable/avatar_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
~ Copyright 2015 Google Inc. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="128dp"
android:height="128dp"
android:viewportHeight="128.0"
android:viewportWidth="128.0">
<path
android:fillColor="#FF8A80"
android:pathData="M0 0h128v128h-128z" />
<path
android:fillColor="#FFE0B2"
android:pathData="M36.3 94.8c6.4 7.3 16.2 12.1 27.3 12.4 10.7,-.3 20.3,-4.7 26.7,-11.6l.2.1c-17,-13.3,-12.9,-23.4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,-16 11.3,-26.7 11.6,-11.2,-.3,-21.1,-5.1,-27.5,-12.6,-.1.2,-.2.4,-.2.5,-3.1.9,-6 2.7,-8.4 5.4l-.2.2s-.5.6,-1.5 1.7c-.9 1.1,-2.2 2.6,-3.7 4.5,-3.1 3.9,-7.2 9.5,-11.7 16.6,-.9 1.4,-1.7 2.8,-2.6 4.3h109.6c-3.4,-7.1,-6.5,-12.8,-8.9,-16.9,-1.5,-2.2,-2.6,-3.8,-3.3,-5z" />
<path
android:fillColor="#444"
android:pathData="M76.3,47.5 m-2.0, 0 a 2.0,2.0 0 1,1 4.0,0 a2.0,2.0 0 1,1 -4.0,0" />
<path
android:fillColor="#444"
android:pathData="M50.7,47.6 m-2.0, 0 a 2.0,2.0 0 1,1 4.0,0 a2.0,2.0 0 1,1 -4.0,0" />
<path
android:fillColor="#444"
android:pathData="M48.1 27.4c4.5 5.9 15.5 12.1 42.4 8.4,-2.2,-6.9,-6.8,-12.6,-12.6,-16.4 17.2 1.5 14.1,-9.4 14.1,-9.4,-1.4 5.5,-11.1 4.4,-11.1 4.4h-18.8c-1.7,-.1,-3.4 0,-5.2.3,-12.8 1.8,-22.6 11.1,-25.7 22.9 10.6,-1.9 15.3,-7.6 16.9,-10.2z" />
</vector>
49 changes: 49 additions & 0 deletions app/src/main/res/drawable/avatar_10.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
~ Copyright 2015 Google Inc. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="128dp"
android:height="128dp"
android:viewportHeight="128.0"
android:viewportWidth="128.0">
<path
android:fillColor="#FFCC80"
android:pathData="M41.6 123.8s0 .1,-.1.1l.3,-.4c-.1.2,-.1.2,-.2.3z" />
<path
android:fillColor="#FFFF8D"
android:pathData="M0 0h128v128h-128z" />
<path
android:fillColor="#C2C2C2"
android:pathData="M83.2 26.6c.1,-.9.2,-1.8.2,-2.7 0,-10.4,-8.4,-18.9,-18.8,-18.9s-18.8 8.4,-18.8 18.8c0 1.4.2 2.7.4 4 5.4,-4 12.2,-6.4 19.4,-6.4 6.5.1 12.5 2 17.6 5.2z" />
<path
android:fillColor="#848484"
android:pathData="M41.4 58.4c9.6,-1.9 10.3,-10.7 11.7,-13.2 4.2 5.4 12.4 16.6 36.8 13.5 1.5,-2.5 4.4,-6 7.4,-5.6.3 0 .6.1 1 .2,-.3,-11.3,-6.3,-21.2,-15.2,-26.8,-5.1,-3.2,-11.1,-5.1,-17.5,-5.1,-7.3 0,-14 2.4,-19.4 6.4,-7.9 5.9,-13.1 15.2,-13.3 25.7.4,-.1.9 1.7 1.4 1.7 2.8,-.4 5.6 1 7.1 3.2z" />
<path
android:fillColor="#FF5722"
android:pathData="M109.6 121.5c-1.2,-2,-2.2,-3.6,-2.9,-4.8l-1.2,-1.8,-.2,-.3c-.5,-.8,-1.1,-1.6,-1.8,-2.3,-3.1,-3.3,-6.9,-5.1,-10.8,-5.3 0 0 .1 0 .1.1l-.2,-.1c-3.4 3.7,-7.8 6.6,-12.7 8.5v12.5h33.4c-1.4,-2.5,-2.6,-4.7,-3.7,-6.5zm-67.6,-15.5l-.2.5c-2.9.8,-5.6 2.5,-7.8 5.1l-.2.2s-.5.6,-1.4 1.6c-.9 1,-2 2.4,-3.5 4.2,-2.1 2.6,-4.7 6.1,-7.5 10.4h28v-15.7c-2.8,-1.7,-5.3,-3.8,-7.4,-6.3z" />
<path
android:fillColor="#E0F7FA"
android:pathData="M67.7 117.7c-6.8,-.2,-13.1,-2.1,-18.3,-5.4v15.7h30.5v-12.6c-3.8 1.4,-7.9 2.2,-12.2 2.3z" />
<path
android:fillColor="#FFE0B2"
android:pathData="M42 106c2.1 2.4 4.6 4.5 7.4 6.3 5.2 3.3 11.5 5.3 18.3 5.4 4.3,-.1 8.4,-.9 12.1,-2.3 5,-1.9 9.3,-4.8 12.7,-8.5l.2.1s-.1 0,-.1,-.1c-15.7,-12.3,-12,-21.7,-7.8,-26.6 1.3,-1.1 2.6,-2.3 4.1,-3.6l12.2,-10.3c1.4,-1.2 2.4,-2.8 2.7,-4.8.5,-3.8,-1.9,-7.3,-5.4,-8.2,-.3,-.1,-.6,-.2,-1,-.2,-3.1,-.4,-5.9 1.1,-7.4 3.6,-24.4 3.1,-32.7,-8.1,-36.8,-13.5,-1.5 2.4,-2.1 11.3,-11.7 13.2,-1.6,-2.3,-4.3,-3.6,-7.2,-3.2l-1.4.3c-3.4 1.1,-5.5 4.5,-5 8.1.3 1.9 1.3 3.6 2.6 4.7l10.2 8.7c5.8 6 14.1 18.3 1.4 30.7,-.1.1,-.1.2,-.1.2zm37.6,-45.3c.8 0 1.5.7 1.5 1.5s-.7 1.5,-1.5 1.5,-1.5,-.7,-1.5,-1.5c-.1,-.9.6,-1.5 1.5,-1.5zm-24 0c.8 0 1.5.7 1.5 1.5s-.7 1.5,-1.5 1.5,-1.5,-.7,-1.5,-1.5c-.1,-.9.6,-1.5 1.5,-1.5z" />
<path
android:fillColor="#444"
android:pathData="M79.6,62.2 m-2.0, 0 a 2.0,2.0 0 1,1 4.0,0 a2.0,2.0 0 1,1 -4.0,0" />
<path
android:fillColor="#444"
android:pathData="M55.6,62.2 m-2.0, 0 a 2.0,2.0 0 1,1 4.0,0 a2.0,2.0 0 1,1 -4.0,0" />
</vector>
43 changes: 43 additions & 0 deletions app/src/main/res/drawable/avatar_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
~ Copyright 2015 Google Inc. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="128dp"
android:height="128dp"
android:viewportHeight="128.0"
android:viewportWidth="128.0">
<path
android:fillColor="#B9F6CA"
android:pathData="M0 0h128v128h-128z" />
<path
android:fillColor="#FFCC80"
android:pathData="M70.1 122.5l.6,-.1c6.1,-.8 12,-2.4 17.7,-4.8 1.2,-.5 2.4,-1.1 3.2,-2.1 1.3,-1.7,-.1,-5.6,-.5,-7.7,-.7,-3.8,-1.3,-7.7,-1.9,-11.5,-.7,-4.5,-1.5,-9.1,-1.6,-13.7,-.2,-7.6.7,-12.3 1.9,-15.3h9l-2.6,-10.4c-.2,-2.4,-.4,-4.8,-.7,-6.8,-.2,-1.9,-.6,-3.6,-1.2,-5.3,-14.9 2.2,-24.5.9,-30.7,-1.8l-23.1 4.5,-.7.1h-.7c-.4,-.1,-.9,-.2,-1.2,-.4,-.4 0,-.9 0,-1.4.1,-4.1.6,-6.9 4.7,-6.3 9.1.3 2 1.2 3.8 2.6 5 .3.1 1.6.7 3.4 1.7.8.4 1.6 1 2.5 1.6 1.5 1.1 3.2 2.5 4.9 4.1 5.8 5.9 8.4 13.8 7.4 22,-.6 4.7,-2.2 9.4,-4.4 13.6,-.5 1,-1 1.6,-1.1 2.8,-.1 1.1,-.1 2.3.1 3.4.4 2.3 1.5 4.4 3 6.2 2.6 3.1 6.4 5 10.4 5.8 3.8.4 7.6.3 11.4,-.1zm9.5,-67.6c.9 0 1.6.7 1.6 1.6 0 .9,-.7 1.6,-1.6 1.6s-1.6,-.7,-1.6,-1.6c-.1,-.8.7,-1.6 1.6,-1.6zM128 97.7c-3.3 1.9,-6.6 3.7,-9.9 5.3,-3.2 1.5,-6.3 2.9,-9.6 4.2,-.9.4,-2.1.5,-2.9 1.1,-1.1.8,-1.9 2.5,-2.3 3.7,-.6 1.6,-.6 3.4.3 4.8.8 1.2 2.1 2 3.5 2.6 5.9 2.9 12.2 5.1 18.6 6.5 1.4.3 2.3 1.8 2.4.1v-28.1c-.1.1,-.1,-.1,-.1,-.2z" />
<path
android:fillColor="#444"
android:pathData="M94.2 44.9c-.8,-2.6,-1.8,-5,-3.2,-7.2l-7.2 1.4,-20.4 4c6.3 2.7 15.9 4 30.8 1.8z" />
<path
android:fillColor="#E65100"
android:pathData="M38.9 48.4h.7c.2 0 .5 0 .7,-.1l23.1,-4.5 20.4,-4 23.3,-4.5c1.9,-.4 3.2,-2 2.9,-3.6,-.3,-1.6,-2.1,-2.6,-4.1,-2.3l-19.6 3.8,-1.3,-6.8c-2,-10.9,-15,-17.7,-29.1,-14.9,-14 2.7,-23.7 13.9,-21.6 24.9h.1l1.7 9v.7c.2.8.7 1.4 1.4 1.9.5.1 1 .3 1.4.4z" />
<path
android:fillColor="#444"
android:pathData="M79.6,56.5 m-2.0, 0 a 2.0,2.0 0 1,1 4.0,0 a2.0,2.0 0 1,1 -4.0,0" />
<path
android:fillColor="#689F38"
android:pathData="M128 128v-1.8l-21.7,-18.2,-.4.2,-2.9 1.3c-3 1.3,-6 2.6,-9.2 3.8l-1.4.5c-9 3.3,-16.5 4.1,-22.8 3.6,-16.4,-1.3,-23.8,-11.9,-23.8,-11.9,-2.2 4.2,-5.2 8.7,-9.2 13.5l-.3.4,-1.7 2c-.9 1.1,-2 2.6,-3.4 4.5,-.4.6,-.9 1.3,-1.4 2l98.2.1z" />
<path
android:fillColor="#FFCC80"
android:pathData="M36.3 119.3s.1,-.2.2,-.3c-.1.1,-.2.2,-.2.3z" />
</vector>
Loading