Skip to content

Commit

Permalink
Cleanup Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
FarshidRoohi committed Oct 8, 2022
1 parent 7cd4932 commit bf740fa
Showing 1 changed file with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,70 @@ import androidx.recyclerview.widget.RecyclerView
import io.github.farshidroohi.extensions.onItemClickListener
import io.github.farshidroohi.extensions.onLoadMoreListener
import ir.farshid_roohi.customadapterrecyclerview.R
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class SingleViewTypeActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView
private val singleViewTypeAdapter = SingleViewTypeAdapter()

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

val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)

val singleViewTypeAdapter = SingleViewTypeAdapter()
recyclerView = findViewById(R.id.recyclerView)

// onClick Error Button
singleViewTypeAdapter.onRetryClicked = {
singleViewTypeAdapter.loadingState()
// fake request or load other items...
recyclerView.postDelayed({
singleViewTypeAdapter.loadedState(tempItems)
}, 2000)
handleOnRetryData()
}

// set Data in List
singleViewTypeAdapter.loadedState(tempItems)
singleViewTypeAdapter.loadedState(fakeItems)

recyclerView.layoutManager = GridLayoutManager(this@SingleViewTypeActivity, 2)
recyclerView.adapter = singleViewTypeAdapter


// set onLoad More Listener for Pagination
recyclerView.onLoadMoreListener {
if (singleViewTypeAdapter.mustLoad) {
singleViewTypeAdapter.loadingState()
// failed state
recyclerView.postDelayed({
singleViewTypeAdapter.failedState()
}, 2000)
}
handleOnLoadMore()
}


// Set onClick Item Listener
recyclerView.onItemClickListener(onClickItem = { position ->
showToast("onClickItem : ${singleViewTypeAdapter.getItem(position)}$position")
}, onLongClickItem = { position ->
showToast("onLongClickItem : ${singleViewTypeAdapter.getItem(position)}$position")
})
recyclerView.onItemClickListener(
onClickItem = { position ->
val item = singleViewTypeAdapter.getItem(position)
showToast("onClickItem : $item")
}, onLongClickItem = { position ->
val item = singleViewTypeAdapter.getItem(position)
showToast("onLongClickItem : $item")
})
}

fun showToast(message: String) {
private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

private fun handleOnLoadMore() {
if (singleViewTypeAdapter.mustLoad) {
singleViewTypeAdapter.loadingState()
// failed state
recyclerView.postDelayed({
singleViewTypeAdapter.failedState()
}, 2000)
}
}

private fun handleOnRetryData() {
singleViewTypeAdapter.loadingState()
// fake request or load other items...
recyclerView.postDelayed({
singleViewTypeAdapter.loadedState(fakeItems)
}, 2000)
}

// Generate Fake Data For List
private val tempItems: List<String>
private val fakeItems: List<String>
get() {
val items: MutableList<String> = ArrayList()
for (i in 0..21) {
Expand Down

0 comments on commit bf740fa

Please sign in to comment.