Reputation: 25
RecyclerView shows first items after some correct items. And one of incorrect items can change own data several times... It is really strange and i dont know why, in another app i wrote the same code and all works nice. I tried to find the resolve, but did not found nothing what helps. You can see video to understand what is going on. video
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_car_list, container, false);
mRecyclerView = view.findViewById(R.id.car_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private class CarHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private Car mCar;
public CarHolder(@NonNull final View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mProducerTextView = itemView.findViewById(R.id.producerCardTextView);
mModelTextView = itemView.findViewById(R.id.modelCardTextView);
mPriceTextView = itemView.findViewById(R.id.priceCardTextView);
}
public void bind(Car car) {
mCar = car;
mProducerTextView.setText(getString(R.string.producer_params,
mCar.getProducer()));
mModelTextView.setText(getString(R.string.model_params,
mCar.getModel()));
mPriceTextView.setText(getString(R.string.price_params,
mCar.getPrice()));
}
@Override
public void onClick(View v) {
mListCallBack.onCarSelected(mCar);
}
}
private class CarAdapter extends RecyclerView.Adapter<CarHolder> {
private List<Car> mCars;
public CarAdapter(List<Car> cars) {
mCars = cars;
}
@NonNull
@Override
public CarHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.list_item_car, viewGroup, false);
return new CarHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CarHolder carHolder, int i) {
Car car = mCars.get(i);
carHolder.bind(car);
}
@Override
public int getItemCount() {
return mCars.size();
}
public void setCars(List<Car> cars) {
this.mCars = cars;
}
}
public void updateUI(){
List<Car> cars = CarLab.getCarLab(getActivity()).getCars();
if (mAdapter == null) {
mAdapter = new CarAdapter(cars);
mRecyclerView.setAdapter(mAdapter);
} else {
mAdapter.setCars(cars);
mAdapter.notifyDataSetChanged();
}
}
Upvotes: 0
Views: 727
Reputation: 1682
The problem occurs when you use the field variable mCar
(which manipulates by all bind events) to update your viewHoleder
, just simply use the car
variable you pass to the bind
to update views.
The other problem with your code is inside onClick
, again you should not use the field variable mCar
, instead get the item it needs like this:
@Override
public void onClick(View v) {
Car car = mCars.get(getAdapterPosition());
mListCallBack.onCarSelected(car);
}
Upvotes: 0
Reputation: 350
may be you can try your RecyclerView not to recyle items using:
mRecyclerView.getRecycledViewPool().setMaxRecycledViews(0,0);
in your onCreate()
method below mRecyclerView.setLayoutManager(...)
;
Upvotes: 3