In my application I want change set new value to position of recyclerview adapter.
With java language I can it with below code:
@Override
public void onBindViewHolder(MonthFragmentAdapter.ViewHolder holder, int position) {
position = 6 - (position % 7) * 2;
}
But in kotlin I can't use below code and show me val error for position.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
position = 6 - position % 7 * 2
}
Show me Val cannot be reassigned! I know I can't set new value for val, but how can I change my code for fix it?
CodePudding user response:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var newPosition = position
newPosition = 6 - newPosition % 7 * 2
}
Use a new variable to change the value.
It is Kotlin
feature, you can't change the functino parameter.
CodePudding user response:
chainging adapter position is not possible, but you can do it with taht way if (position == yourposition){//do that}