Created
May 3, 2023 22:56
-
-
Save booknara/c99fccc326d5d56fc2ebae74ca94fec0 to your computer and use it in GitHub Desktop.
ListAdapter which has RecyclerView + DiffUtil.ItemCallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// build.gradle file | |
// Recycler View (ConcatAdapter) | |
implementation 'androidx.recyclerview:recyclerview:1.3.0' | |
// HeroAdapter class | |
class HeroAdapter(private val context: Context) : | |
ListAdapter<Hero, HeroAdapter.HeroViewHolder>(HeroDiffCallback) { | |
private var currentPosition: Int = 0 | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeroViewHolder { | |
val view = LayoutInflater.from(context).inflate(R.layout.list_layout_heroes, parent, false) | |
return HeroViewHolder(view) | |
} | |
override fun onBindViewHolder(holder: HeroViewHolder, position: Int) { | |
val hero = getItem(position) | |
holder.bind(context, hero, currentPosition, position) | |
holder.textViewName.setOnClickListener { | |
currentPosition = position | |
notifyDataSetChanged() | |
} | |
} | |
class HeroViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
val textViewName: TextView = itemView.findViewById(R.id.textViewName) | |
private val textViewRealName: TextView = itemView.findViewById(R.id.textViewRealName) | |
private val textViewTeam: TextView = itemView.findViewById(R.id.textViewTeam) | |
private val textViewFirstAppearance: TextView = itemView.findViewById(R.id.textViewFirstAppearance) | |
private val textViewCreatedBy: TextView = itemView.findViewById(R.id.textViewCreatedBy) | |
private val textViewPublisher: TextView = itemView.findViewById(R.id.textViewPublisher) | |
private val textViewBio: TextView = itemView.findViewById(R.id.textViewBio) | |
private val imageView: ImageView = itemView.findViewById(R.id.imageView) | |
private val linearLayout: LinearLayout = itemView.findViewById(R.id.linearLayout) | |
fun bind(context: Context, hero: Hero, currentPosition: Int, position: Int) { | |
textViewName.text = hero.name | |
textViewRealName.text = hero.realname | |
textViewTeam.text = hero.team | |
textViewFirstAppearance.text = hero.firstappearance | |
textViewCreatedBy.text = hero.createdby | |
textViewPublisher.text = hero.publisher | |
textViewBio.text = hero.bio | |
Glide.with(context).load(hero.imageurl).into(imageView) | |
linearLayout.visibility = View.GONE | |
if (currentPosition == position) { | |
val animation = AnimationUtils.loadAnimation(context, R.anim.slide_down) | |
linearLayout.visibility = View.VISIBLE | |
linearLayout.animation = animation | |
} | |
} | |
} | |
object HeroDiffCallback : DiffUtil.ItemCallback<Hero>() { | |
override fun areItemsTheSame(oldItem: Hero, newItem: Hero): Boolean { | |
return oldItem.name == newItem.name | |
} | |
override fun areContentsTheSame(oldItem: Hero, newItem: Hero): Boolean { | |
return oldItem == newItem | |
} | |
} | |
} | |
// MainActivity class | |
class MainActivity : AppCompatActivity() { | |
private lateinit var heroHeaderAdapter: HeroHeaderAdapter | |
private lateinit var heroAdapter: HeroAdapter | |
private lateinit var concatAdapter: ConcatAdapter | |
private lateinit var recyclerView: RecyclerView | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
recyclerView = findViewById(R.id.recyclerViewHeroes) | |
recyclerView.setHasFixedSize(true) | |
recyclerView.layoutManager = LinearLayoutManager(this) | |
getHeroes() | |
} | |
private fun getHeroes() { | |
val call = RetrofitClient.heroApi.getHeroes() | |
call.enqueue(object : Callback<List<Hero>> { | |
override fun onResponse(call: Call<List<Hero>>, response: Response<List<Hero>>) { | |
val heroList = response.body() | |
heroList?.let { list -> | |
heroAdapter = HeroAdapter(this@MainActivity) | |
heroHeaderAdapter = HeroHeaderAdapter(listOf(list.size.toString()), this@MainActivity) | |
concatAdapter = ConcatAdapter(heroHeaderAdapter, heroAdapter) | |
recyclerView.adapter = concatAdapter | |
heroAdapter.submitList(list) // Wired with data | |
} | |
} | |
override fun onFailure(call: Call<List<Hero>>, t: Throwable) { | |
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show() | |
} | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment