我想从MainActivity访问AdapterViewPager中的按钮
//这是AdapterViewPager
public class AdapterViewPagerHot extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.cat_mermaid_cartoon_grumpy_cat_94004_1280x720,R.drawable.catnrobot,R.drawable.kleptocats,R.drawable.magiccat,R.drawable.flyingcat};
String url[] = new String[]{
"https://i.chzbgr.com/full/8803531264/hD42E57C6/",
"https://i.pinimg.com/originals/d2/fc/5a/d2fc5ab22b9b9d13320b3a8be48625ad.jpg",
"https://i.ytimg.com/vi/5rOtNmdKgsE/maxresdefault.jpg"
};
public AdapterViewPagerHot(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.item_viewpagerdisplay, null);
ImageView imageView = v.findViewById(R.id.imageView);
final ImageView ICONHotViewPagerSaving = v.findViewById(R.id.ICON_HotViewPagerSaving);
imageView.setImageResource(images[position]);
/*Glide.with(getActivity()).load(image_url).into(circleimageviewPicture);*/
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Slide" + (position+1) + "Clicked", Toast.LENGTH_SHORT).show();
}
});
final Boolean[] boollatestnewssaving = {false};
ICONHotViewPagerSaving.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (boollatestnewssaving[0] == true){
Toast.makeText(v.getContext(), "Turn Off Saved News ", Toast.LENGTH_SHORT).show();
ICONHotViewPagerSaving.setImageResource(R.drawable.saved_off);
boollatestnewssaving[0] = false;
/*updateSavedNewsSaving("update_newssaving", id, false);*/
} else if(boollatestnewssaving[0] == false) {
Toast.makeText(v.getContext(), "Turn On Saved News ", Toast.LENGTH_SHORT).show();
ICONHotViewPagerSaving.setImageResource(R.drawable.saved_on);
boollatestnewssaving[0] = true;
/*updateSavedNewsSaving("update_newssaving", id, true);*/
}
}
});
ViewPager vp = (ViewPager) container;
vp.addView(v, 0);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
//我要访问
Main Activity.java
OnCreate(){
//在此里面 }