我有一个带适配器的常规ViewPager:
public class MyDialogFragment extends DialogFragment {
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private City city;
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewPager);
city = Parcels.unwrap(getArguments().getParcelable("city"));
myViewPagerAdapter = new MyViewPagerAdapter(getContext());
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// Page changed
}
});
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(city.getImage())
.into(imageView);
}
container.addView(view);
return view;
}
@Override
public int getCount() {
return city.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
正如您所看到的,我在ViewPager
上设置了一个页面更改侦听器。但是我需要能够在页面发生变化时通知适配器,以便我可以修改适配器的ImageView
。我怎么能这样做?
答案 0 :(得分:0)
向适配器类添加方法
pm2 logs
在public class MyViewPagerAdapter extends PagerAdapter {
...
public void pageChanged(int position) {
// Do something
}
...
}
之后使用该方法
OnPageChangeListener
答案 1 :(得分:0)
一种方法是在适配器中创建数据结构,以将寻呼机视图映射到其位置。 SparseArray
效率最高:
private SparseArray<View> pageMap = new SparseArray<>();
然后在适配器的instantiateItem()
方法中,将视图添加到SparseArray
:
public Object instantiateItem(ViewGroup container, int position) {
...
pageMap.put(position, view);
...
}
现在,在您的OnPageChangeListener
中,您可以执行以下操作:
@Override
public void onPageSelected(int position) {
View view = adapter.getView(pos);
if (view != null) {
ImageView iv = (ImageView) view.findViewById(R.id.your_image_view);
// You now have the image view
}
}
请务必从适配器SparseArray
方法中的destroyItem()
删除观看次数:
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
pageMap.remove(position);
...
}
答案 2 :(得分:0)
您可以使用带有imagelist的构造函数来传递并在下一页自动调用..在您的代码中将被修改为::: ArrayList imageList = new ArrayList&lt;&gt;();
imageList.add("ImagePath here");
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
然后你的适配器就像::
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private ArrayList<String> imageList
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context,ArrayList<String> imageList) {
this.context = context;
this.imageList = imageList;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(imageList.get(position)) // This will be changes like
.into(imageView);
}
container.addView(view);
return view;
}
@Override
public int getCount() {
return city.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
那样做.. 在此之后,有一些建议 - 请在使用viewpager时使用arraylist .. 现在你只是从包中取出parceble对象.. 你可以为arraylist做同样的事, 通过立即传递整个arraylist来接收来自同一捆绑的arraylist。 所以你不需要找到页面更改事件..viewpager会自动处理.. 这里是如何发送&amp;收到可分辨的arraylist
// while sending the list
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("citylist", new ArrayList<City>());
// while receiving the list
ArrayList<City> citylist = getIntent().getParcelableArrayListExtra("citylist");
之后只需获取所有图像并添加到imagelist数组
for (int i = 0; i < citylist.size(); i++) {
imageList.add(citylist.get(i).getImage());
}
并在
中传递此imageList
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
告诉我是否需要更多的帮助。