i am trying to center selected item from RecyclerView
i have this code from Fragment to call the API interface and the recyclerView
val recyclerView3: RecyclerView = view.findViewById(R.id.list_recycler3)
recyclerView3.layoutManager = LinearLayoutManager(
requireContext(),
LinearLayoutManager.HORIZONTAL,
false
)
val findercatService = findercatService.create().getServices()
findercatService.enqueue(object : Callback<List<ilmcategoriesData>> {
override fun onResponse(
call: Call<List<ilmcategoriesData>>?,
response: Response<List<ilmcategoriesData>>?
) {
var list = response?.body()!!
val arrayList = ArrayList<ilmcategoriesData>()
arrayList.addAll(list)
list = arrayList
recyclerView3.adapter =
ilmcategoriesAdapter(
list,
MainActivity(),
object : ilmcategoriesAdapter.OnClickRecyclerChild {
override fun myCallback(s: String?) {
when (s.toInt()) {
26 -> {
AppPreferences.heightInCentimeters =
1; AppPreferences.birthdayInMilliseconds = "none"
}
27 -> {
AppPreferences.heightInCentimeters =
2; AppPreferences.birthdayInMilliseconds = "none"
}
28 -> {
AppPreferences.heightInCentimeters =
3; AppPreferences.birthdayInMilliseconds = "none"
}
29 -> {
AppPreferences.heightInCentimeters =
4; AppPreferences.birthdayInMilliseconds = "none"
}
30 -> {
AppPreferences.heightInCentimeters =
5; AppPreferences.birthdayInMilliseconds = "none"
}
31 -> {
AppPreferences.heightInCentimeters =
6; AppPreferences.birthdayInMilliseconds = "none"
}
32 -> {
AppPreferences.heightInCentimeters =
7; AppPreferences.birthdayInMilliseconds = "none"
}
33 -> {
AppPreferences.heightInCentimeters =
8; AppPreferences.birthdayInMilliseconds = "none"
}
34 -> {
AppPreferences.heightInCentimeters =
9; AppPreferences.birthdayInMilliseconds = "none"
}
35 -> {
AppPreferences.heightInCentimeters =
10; AppPreferences.birthdayInMilliseconds = "none"
}
36 -> {
AppPreferences.heightInCentimeters =
11; AppPreferences.birthdayInMilliseconds = "none"
}
37 -> {
AppPreferences.heightInCentimeters =
12; AppPreferences.birthdayInMilliseconds = "none"
}
38 -> {
AppPreferences.heightInCentimeters =
13; AppPreferences.birthdayInMilliseconds = "none"
}
39 -> {
AppPreferences.heightInCentimeters =
14; AppPreferences.birthdayInMilliseconds = "none"
}
else -> { // Note the block
AppPreferences.heightInCentimeters = 0
}
}
if (AppPreferences.birthdayInMilliseconds == "fav") {
getToken()
val finderService = finderService.create().getServices(token)
finderService.enqueue(object : Callback<List<IlmFinders>> {
override fun onResponse(
call: Call<List<IlmFinders>>?,
response: Response<List<IlmFinders>>?
) {
if (response != null) {
if (response.isSuccessful) {
val arrayList = ArrayList<IimfinderData>()
val allfinders = response.body()?.get(0)
for (fav in allfinders?.ilmFinders!!) {
if (fav.isFavourite)
arrayList.add(fav)
}
Log.d(
TAG,
"onResponse: favorite size = ${arrayList.size}"
)
var newAdapter =
FavoriteAdapter(
arrayList,
MainActivity()
)
recyclerView6.adapter =
newAdapter
newAdapter.notifyDataSetChanged()
}
}
}
override fun onFailure(
call: Call<List<IlmFinders>>?,
t: Throwable?
) {
}
})
} else {
val finderService = finderService.create().getServices()
finderService.enqueue(object : Callback<List<IlmFinders>> {
override fun onResponse(
call: Call<List<IlmFinders>>?,
response: Response<List<IlmFinders>>?
) {
val list = response?.body()!!
val newAdapter =
IimfinderAdapter(list, MainActivity())
recyclerView6.adapter =
IimfinderAdapter(list, MainActivity())
newAdapter.notifyDataSetChanged()
}
override fun onFailure(
call: Call<List<IlmFinders>>?,
t: Throwable?
) {
}
})
}
}
})
}
override fun onFailure(call: Call<List<ilmcategoriesData>>?, t: Throwable?) {
}
})
Adapter:
class ilmcategoriesAdapter(
var countryList: List<ilmcategoriesData>,
var activity: MainActivity,
var listener: OnClickRecyclerChild
) : RecyclerView.Adapter<ilmcategoriesAdapter.ViewHolder>() {
var prevIndex = -1
init {
val arrayList = ArrayList<ilmcategoriesData>()
arrayList.addAll(countryList)
arrayList.add(0, ilmcategoriesData(41, "All Categories"))
arrayList.add(1, ilmcategoriesData(42, "Favorite"))
countryList = arrayList
}
lateinit var context: Context
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
context = parent.context!!
val view = LayoutInflater.from(context).inflate(R.layout.list_item3, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.eventName.text = countryList[position].name
holder.eventName.setOnClickListener {
notifyDataSetChanged()
prevIndex = position
var CategoryID = countryList[position].id
var CategoryIdStr = CategoryID.toString()
when (CategoryID) {
41 -> {
AppPreferences.birthdayInMilliseconds = "all"
}
42 -> {
AppPreferences.birthdayInMilliseconds = "fav"
}
else -> AppPreferences.birthdayInMilliseconds = "none"
}
listener.myCallback(CategoryIdStr)
if (position == prevIndex) {
holder.eventName.background =
ContextCompat.getDrawable(context, R.drawable.iimfinder_selector)
holder.eventName.setTypeface(null, Typeface.BOLD)
} else {
holder.eventName.background = ContextCompat.getDrawable(context, R.color.white)
holder.eventName.setTypeface(null, Typeface.NORMAL)
}
}
if (position == prevIndex) {
holder.eventName.setTypeface(null, Typeface.BOLD)
holder.eventName.background =
ContextCompat.getDrawable(context, R.drawable.iimfinder_selector)
}
else if(position == 0) {
holder.eventName.setTypeface(null, Typeface.BOLD)
holder.eventName.background =
ContextCompat.getDrawable(context, R.drawable.iimfinder_selector)
}
else {
holder.eventName.setTypeface(null, Typeface.NORMAL)
holder.eventName.background = ContextCompat.getDrawable(context, R.color.white)
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val eventName: TextView = itemView.findViewById(R.id.cat_title)
}
interface OnClickRecyclerChild {
fun myCallback(s: String?)
}
}
to show these items from API and once the user click on an item the item will auto center in the screen
i tried to add snaphelper in the fragment but it didn't work
