У меня есть RecyclerView. У Recycler есть 10 элементов. Каждый элемент имеет панель Progressbar. Могу ли я получить доступ к этой панели прогресса из MainActivity. Я пытаюсь сделать это с помощью обратного вызова, но это приводит к проблемам, потому что индикаторы выполнения из разных элементов начали работать. Maybe ProgressBars начинает инициализироваться после прокрутки. вниз.
Вот мой AdapterClass.
public class MyHospitalAdapter extends RecyclerView.Adapter<MyHospitalAdapter.MyViewHolder> {
private List<Hospital> mHospitalList;
private Context context;
public onImageClickListener mCallBack;
public interface onImageClickListener {
void setProgressBars(int id,ProgressBar progressBar);
void clickOnImage(int id);
void onTextViewOfPriceSelected(Hospital hospital);
void startHospital(int id);
void finishTimer(int id,int value);
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
ImageView imageView;
ProgressBar progressBar;
TextView textViewOnProgressBar;
Button price;
TextView productionAmount;
TextView nameHospital;
public MyViewHolder(@NonNull View itemView, ImageView imageView, ProgressBar progressBar, TextView textViewOnProgressBar, Button price, TextView productionAmount, TextView nameHospital) {
super(itemView);
this.imageView = imageView;
this.progressBar = progressBar;
this.textViewOnProgressBar = textViewOnProgressBar;
this.price = price;
this.productionAmount = productionAmount;
this.nameHospital = nameHospital;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyHospitalAdapter(List<Hospital> mHospitalsList, Context context, onImageClickListener listener) {
this.mHospitalList = mHospitalsList;
this.context = context;
this.mCallBack = listener;
// mCallBack = (onImageClickListener) context;
}
// Create new views (invoked by the layout manager)
@Override
public MyHospitalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
OneBusinesBinding binding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.one_busines, parent, false);
MyViewHolder vh = new MyViewHolder(binding.constraintLayout, binding.imageBusiness, binding.progressBar, binding.textViewOnProgressbar, binding.price, binding.amountofProduction, binding.nameHospital);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Hospital currentItem = mHospitalList.get(position);
holder.imageView.setImageResource(AndroidImageAssets.getPictures().get(position));
holder.productionAmount.setText(context.getString(R.string.amount_of_production, currentItem.getAmount()));
holder.price.setText(context.getString(AssetsUpgradeStrings.getStrings_On_Button_Buy().get(position), convertNumberToString(currentItem.getPrice())));
holder.progressBar.setProgressTintList(ColorStateList.valueOf(context.getColor(R.color.blue)));
holder.nameHospital.setText(AssetsUpgradeStrings.getHospitalsNames().get(position));
holder.textViewOnProgressBar.setText(context.getString(R.string.string_on_progressbar, currentItem.getAmount() * currentItem.getMultiplier()));
mCallBack.setProgressBars(position, holder.progressBar);
Animation zoom = AnimationUtils.loadAnimation(context, R.anim.zoomin);
if (currentItem.getAmount() != 0) {
holder.imageView.setAnimation(zoom);
}
holder.imageView.setOnClickListener((v) -> {
mCallBack.clickOnImage(position);
});
holder.price.setOnClickListener((v) -> {
if (MainActivity.mAllMoney >= currentItem.getPrice()) {
currentItem.setAmount(currentItem.getAmount() + 1);
MainActivity.mAllMoney -= currentItem.getPrice();
currentItem.setPrice((int) Math.ceil(MyConstants.getPrices().get(position) * Math.pow(1.12, currentItem.getAmount())));
mCallBack.onTextViewOfPriceSelected(currentItem);
holder.price.setText(context.getString(AssetsUpgradeStrings.getStrings_On_Button_Buy().get(position), convertNumberToString(currentItem.getPrice())));
holder.productionAmount.setText(context.getString(R.string.amount_of_production, currentItem.getAmount()));
holder.textViewOnProgressBar.setText(context.getString(R.string.string_on_progressbar, currentItem.getAmount() * currentItem.getMultiplier()));
MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.button_click);
mPlayer.setVolume(30, 30);
mPlayer.start();
if(currentItem.getAmount() == 1){
holder.imageView.setAnimation(zoom);
}
}
});
if (currentItem.getValueProgressBar() != 0) {
mCallBack.finishTimer(position, currentItem.getValueProgressBar());
currentItem.setValueProgressBar(0);
}
mCallBack.startHospital(position);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mHospitalList.size();
}
Вот мой MainActivityClass
RecyclerView recyclerView = binding.recyclerView;
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter mAdapter = new MyHospitalAdapter(mListHospitals, getApplicationContext(), new MyHospitalAdapter.onImageClickListener() {
@Override
public void setProgressBars(int id, ProgressBar progressBar) {
ProgressBarsList.set(id,progressBar);
}
@Override
public void clickOnImage(int id) {
if (hospitalViewModel.getAllMyHospitals().get(id).getAmount() == 0)
return;
if (hospitalViewModel.getmManagerList().get(id).getState() != 0) {
return;
}
startTimer(id);
}
@Override
public void onTextViewOfPriceSelected(Hospital hospital) {
DataGame dataGame = new DataGame(0, isStartedNewGame, mAllMoney, mAllPeople, mSickPeople, mDeadPeople, mDays, mGrowthPatiens, mGrowthDeadPatiens);
hospitalViewModel.updateMainDataGame(dataGame);
hospitalViewModel.updateHospital(hospital);
}
@Override
public void startHospital(int id) {
if (hospitalViewModel.getmManagerList().get(id).getState() > 0) {
EndLessTimerHospitalsList.set(id, new EndLessTimer(10000000000L, hospitalViewModel.getAllMyHospitals().get(id).getTime() / 100));
EndLessTimerHospitalsList.get(id).setId(id);
EndLessTimerHospitalsList.get(id).start();
}
}
@Override
public void finishTimer(int id ,int value) {
isHospitalRunning[id] = true;
isStartedTimer[id] = true;
int time = hospitalViewModel.getAllMyHospitals().get(id).getTime();
FinishTimer finishTimer = new FinishTimer(time * 10, time / 100);
finishTimer.setId(id);
finishTimer.setValue(value);
finishTimer.start();
}
});
recyclerView.setAdapter(mAdapter);
Вы можете мне помочь? Как использовать ProgressBar от MainActivity. Я могу получить доступ к прогрессбар. Но это приводит к проблемам. Я думаю, что получаю плохой доступ.