android 如何在ViewPager中更改其他布局的值

a5g8bdjr  于 2023-04-10  发布在  Android
关注(0)|答案(1)|浏览(144)

我的viewPager有两种布局。当我在第一个布局中时,我不能更改第二个布局的值。我如何才能到达另一个布局的组件?
主要活动

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) {

    }
};

我的适配器(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);
}

我在instantiateItem函数中创建了一个两个视图。当应用程序启动时,它在位置〈10时工作。但我想改变我在第一个位置时的情况。

wn9m85ua

wn9m85ua1#

ViewPager的构造函数传入一个回调对象,并让Activity处理回调。在回调中,您可以根据需要更新其他布局。

相关问题