I have a counter view which should update as user touch minus
and plus
buttons.
I'm trying to update counter within ViewPager's adapter instantiateItem
method, but it doesn't make any effect.
I can update it's value only in instantiateItem
. Is there any way to setText()
outside of instantiateItem
? Sorry for dumb question, i'm a newbie in Android.
public class OrderVPAdapter extends PagerAdapter implements View.OnClickListener {
private ArrayList<SomeItem> list;
private Context context;
public TextView counterView;
View view;
// I skipped some code for better understanding
int counter = 0;
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.vp_item, container, false);
counterView = view.findViewById(R.id.counter);
Button plus, minus;
plus = view.findViewById(R.id.btn_plus);
plus.setOnClickListener(this);
minus = view.findViewById(R.id.btn_minus);
minus.setOnClickListener(this);
container.addView(view, 0);
return view;
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.btn_plus) {
counter++;
}
else {
if(counter > 0)
counter--;
}
counterView.setText(counter);
}