In my application I have one fragment and in this fragment I have one recyclerview and 2 button.
Button 1 is Select all and button 2 is Deselect all.
I write below codes for recyclerview:
`
private lateinit var binding: ItemDayMyBinding
private lateinit var context: Context
private val TYPE_HEADER = 0
private val TYPE_DAY = 1
private var firstDayDayOfWeek = 0
private var totalDays = 0
private var todayPosition = -1
private val addedDateList = mutableListOf<PersianDate>()
private val removedDateList = mutableListOf<PersianDate>()
private val selectedDays = mutableListOf<DayEntity>()
init {
firstDayDayOfWeek = items[0].dayOfWeek
totalDays = items.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding = ItemDayMyBinding.inflate(LayoutInflater.from(parent.context), parent, false)
context = parent.context
return ViewHolder()
}
override fun getItemCount() = 7 * 7
override fun getItemViewType(position: Int): Int {
return if (isPositionHeader(position)) {
TYPE_HEADER
} else {
TYPE_DAY
}
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
holder.setIsRecyclable(false)
}
inner class ViewHolder : RecyclerView.ViewHolder(binding.root) {
//Bind data
fun bind(pos: Int) {
binding.apply {
var position = pos
position += 6 - (position % 7) * 2
//Check
if (totalDays < position - 6 - firstDayDayOfWeek) {
return
} else {
//Day
if (!isPositionHeader(position)) {
if (position - 7 - firstDayDayOfWeek >= 0) {
val day = items[position - 7 - firstDayDayOfWeek]
dayTxt.isVisible = true
//Day text options
dayTxt.text = day.num
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.black))
//Holiday
if (day.isHoliday) {
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.mystic))
} else {
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.black))
//Today
if (day.isToday) {
todayPosition = position
}
//Previous of today
if (position < todayPosition) {
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.spanishGray))
} else {
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.black))
}
}
} else {
dayTxt.isVisible = false
}
} else {
//Header
dayTxt.text = NAME_OF_DAYS_OF_WEEK_NAME[position]
dayTxt.setTextColor(ContextCompat.getColor(context, R.color.weldonBlue))
}
}
}
}
}
private fun isPositionHeader(position: Int) = position < 7
I want when click on select all button, select all today and next days.
Not previous days!
Searched in stackoverflow and find this:
`
ArrayList<Item> selected = new ArrayList<Item>();
ArrayList<Item> items = new ArrayList<Item>();
public void selecteAll() {
selected.clear();
selected.addAll(items);
notifyDataSetChanged();
}
public void clearAll() {
selected.clear();
notifyDataSetChanged();
}
public void bindView() {
Item item = items.get(position);
if(selected.contains(item) {
// Do selected action
} else {
// Non selecetd ctions
}
}
`
In this solution just select all of items, but I want just select today and next days!
How can I it?
>Solution :
Not getting exactly what you have in Item and other data but yes below code can give you hint what you can do
I want when click on select all button, select all today and next days.
As per your requirement , you first need to determine that you need to perform selection operation or not and from where
So first you declare one boolean in your adapter class
private boolean needToSelectDays = false
after declaring it in adapter , you can make change in selectAll function like this
public void selecteAll() {
needToSelectDays = true
notifyDataSetChanged();
}
public void clearAll() {
needToSelectDays = false
notifyDataSetChanged();
}
in above code we are managing boolean variable only .. now it is time of use it and as per your code , you are binding data in ViewHolder class so you can add this code within your existing code
fun bind(pos: Int) {
// your existing code
if(needToSelectDays && dayGettingFromItem >= currentDay){
// you can write here logic of selecting date
}else{
// you can write here logic of unselecting dat
}
}
hope it will help you