安卓广告轮播图的小圆点是radioButton,在活动中设置backgroundResource的时候可以改变背景颜色,但是在fragment中的onresume调用却不行
// 轮播图,主活动中设置
@SuppressLint({"MissingInflatedId", "LocalSuppress"}) ViewPager2 viewPager2 =findViewById(R.id.viewpager);
viewPager2.setAdapter(new Adapter(MainActivity.this));
RadioButton button=findViewById(R.id.dot2);
button.setBackgroundResource(R.drawable.dot_selected);
//fragment中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v1=LayoutInflater.from(context).inflate(R.layout.fragment_home, null, false);
dots.add(v1.findViewById(R.id.dot1));
dots.add(v1.findViewById(R.id.dot2));
dots.add(v1.findViewById(R.id.dot3));
dots.add(v1.findViewById(R.id.dot4));
dots.add(v1.findViewById(R.id.dot5));
View v=inflater.inflate(R.layout.recycle_picture_fragment, container, false);
ImageView view=v.findViewById(R.id.imgView);
view.setImageResource(imgs.get(index));
return v;
}
@Override
public void onResume() {
Log.d("OnResume","Onresume调用");
super.onResume();
Log.d("LIST",dots.size()+"");
dots.get(0).setBackgroundResource(R.drawable.dot_selected);
// dots.get(adapter.nowIndex).setBackgroundResource(R.drawable.dot_unselected);
// adapter.nowIndex=index;
// dots.get(adapter.nowIndex).setBackgroundResource(R.drawable.dot_selected);
}
我是想在viewpage2切换fragment时,在frag中清除上个小圆点的选中状态,同时设置对应当前frag小圆点的选中状态。但是设置在frag中设置没反应。