如何在 ViewPager 中更改另一个布局的值
How can I change another layout's value in ViewPager
我有一个 viewPager
有两种 layouts
。当我在第一个 layout
时,我无法更改第二个 layout
值。我怎样才能到达另一个 layout's
组件。
主要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) {
}
};
我的适配器 (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
函数中创建了两个视图。当应用程序启动时,它会在 position < 10
时工作。但是当我在第一位的时候我想改变。
将回调对象传递给 ViewPager
的构造函数,让 activity 处理回调。在回调中,您可以根据需要更新其他布局。
我有一个 viewPager
有两种 layouts
。当我在第一个 layout
时,我无法更改第二个 layout
值。我怎样才能到达另一个 layout's
组件。
主要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) {
}
};
我的适配器 (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
函数中创建了两个视图。当应用程序启动时,它会在 position < 10
时工作。但是当我在第一位的时候我想改变。
将回调对象传递给 ViewPager
的构造函数,让 activity 处理回调。在回调中,您可以根据需要更新其他布局。