javaandroidandroid-viewpagerandroid-pageradapter

How can I change another layout's value in ViewPager


I have a viewPager with two types of layouts. When I am in first layout I cannot change second layout value. How can I reach another layout's component.

Main Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sorag);

    
    slideViewPager = findViewById(R.id.slideViewPager);

    sliderAdapter = new SliderAdapter(this, bilet , slideViewPager, );
    slideViewPager.setAdapter(sliderAdapter);
    slideViewPager.setCurrentItem(mCurrentSorag);
    slideViewPager.setOffscreenPageLimit(11);
 
  
    slideViewPager.addOnPageChangeListener(viewListener);

}

ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mCurrentSorag = position;;

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
};

My adapter (PagerAdapter)

@Override
public int getCount() {
    return 11;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == object;
}

@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
    LayoutInflater layoutInflater;
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    assert layoutInflater != null;
    final View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
    final View viewResult = layoutInflater.inflate(R.layout.slide_layout_Result, container, false);
    
    if(position<10) {

        textViewStatusResult = viewResult.findViewById(R.id.textViewStatusLast);
        textViewResult = viewResult.findViewById(R.id.textViewResultLast);

        textViewStatusResult.setText("Not changed textView");
        
            button.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("RestrictedApi")
                @Override
                public void onClick(View v) {

                   // onButtonClicked(button, position, currentButton, view);

                }
            });

        container.addView(view);
        container.addView(viewResult);
        return view;
    }else{

        textViewStatusResult = viewResult.findViewById(R.id.textViewStatusLast);
        textViewResult = viewResult.findViewById(R.id.textViewResultLast);
        textViewResult.setText("1 / 10");
        }

        container.addView(viewResult);
        return viewResult;

}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((LinearLayout)object);
}

I create a two View in instantiateItem function. When application start it works when position < 10. But I want to change when I am in first position.


Solution

  • Pass in a callback object to your ViewPager's constructor and let your activity handle the callback. In the callback you can update the other layout as needed.