0

I have Activity A with recyclerView - when I click on some item (e.g. item on place number 20) then new Activity B is open. On the second activity I click on button and return back to the previous Activity A. Layout of RecyclerView is different - now I can see items from starting order (from first position) and not from positin 21 (because I need to highlight next item).

This is adapter:

class PolozkaAdapter(val chosen_item: Int, private val listener: OnItemClickListener): ListAdapter<Polozka, PolozkaAdapter.PolozkaViewHolder>(DiffCallback()){
    var selectedItemPosition: Int = chosen_item    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PolozkaViewHolder {
        val binding = PolozkyItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PolozkaViewHolder(binding)
        
    }
    override fun onBindViewHolder(holder: PolozkaViewHolder, position: Int) {
        val currentItem = getItem(position)
        holder.bind(currentItem)
        if (selectedItemPosition == position){
            holder.itemView.setBackgroundColor(Color.parseColor("#DA745A"))
        } else
        {
            holder.itemView.setBackgroundColor(Color.TRANSPARENT)
        }
    }    
    inner class PolozkaViewHolder(private val binding: PolozkyItemBinding): RecyclerView.ViewHolder(binding.root){
        init {
            binding.root.setOnClickListener{
                val position = bindingAdapterPosition
                if (position != RecyclerView.NO_POSITION){
                    val item = getItem(position)
                    if (item != null){
                        listener.onItemClick(item, position)                        
                    }
                }
                notifyItemChanged(selectedItemPosition)                
                selectedItemPosition = bindingAdapterPosition
                notifyItemChanged(selectedItemPosition)
            }            
        }
        fun bind(polozkaPolozka: Polozka){
            binding.apply {                
                tvREG.text = polozkaPolozka.reg
                tvVB.text = polozkaPolozka.veb.toString()                
            }
        }        
    }
    interface OnItemClickListener{
        fun onItemClick(polozkaDoklad: Polozka, position: Int)    
    }
    class DiffCallback: DiffUtil.ItemCallback<Polozka>(){
        override fun areItemsTheSame(oldItem: Polozka, newItem: Polozka) =
            oldItem.pvp06pk == newItem.pvp06pk
        override fun areContentsTheSame(oldItem: Polozka, newItem: Polozka) =
            oldItem == newItem
    }
}

And this is how I call it in onCreate method

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityPolozkaBinding.inflate(layoutInflater)
        idPositionItem = intent.getIntExtra("PositionItemPlus",0)
        val polozkaAdapter = PolozkaAdapter(idPositionItem, this)
        binding.apply {
            recyclerViewObjednavka.apply {
                adapter = polozkaAdapter
                layoutManager = LinearLayoutManager(this@PolozkaActivity)
            }
            polozkaViewModel.getall(index,idExp.toString() ).observe(this@PolozkaActivity){
                polozkaAdapter.submitList(it)

            }
        }

IdPostionItem is raised in second activity by one - because I want to highlight other item in recyclerview. maxSize is number of items in data class.

            if (idPositionItem == maxSize) {
                idPositionItem = idPositionItem
            } else{
                idPositionItem = idPositionItem + 1
            }
jenik2205
  • 339
  • 1
  • 3
  • 14

0 Answers0