ViewPager刷新解析
原本以为PagerAdapter类似于BaseAdapter 但是使用起来发现刷新还是有区别的 解析如下
先上Demo
PagerAdapter的四大函数
getCount 返回要滑动的View的个数
isViewFromObject 从当前container中删除指定位置(position)的View
instantiateItem 将当前视图添加到container中 返回当前View
destroyItem
第一个问题 但数据源发生变化时 调用viewPagerAdapter.notifyDataSetChanged() 发现无法刷新数据
解决办法 PagerAdapter中重写getItemPosition方法
上代码
public class MainActivity extends Activity { private ViewPager view_pager; //预览照片的分页显示 private ViewPagerAdapter viewPagerAdapter; //分页图片的预览器 private ArrayList<String> fileNames = new ArrayList<String>(); //本地图片路径 private int currentPosition; //当前选择的图片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } // 获取本地所有照片路径 private void initData() { fileNames.clear(); Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //图片的保存位置的数据 fileNames.add(new String(data, 0, data.length - 1)); } } //初始化控件 private void initView() { view_pager = (ViewPager) findViewById(R.id.view_pager); viewPagerAdapter = new ViewPagerAdapter(this, fileNames); view_pager.setAdapter(viewPagerAdapter); view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int i) { currentPosition = i; Log.i("mbk", "翻页后的当前页面" + currentPosition); } @Override public void onPageScrolled(int i, float f, int j) { } @Override public void onPageScrollStateChanged(int i) { } }); Button delete = (Button)findViewById(R.id.delete); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (fileNames.size() > 0) { fileNames.remove(currentPosition); } Log.i("mbk", "删除的当前页面" + currentPosition); viewPagerAdapter.notifyDataSetChanged(); } }); } }
public class ViewPagerAdapter extends PagerAdapter{ private Context context; private ArrayList<String> fileNames = new ArrayList<String>(); //本地图片路径 public ViewPagerAdapter(Context context, ArrayList<String> fileNames) { super(); this.context = context; this.fileNames = fileNames; } @Override public int getCount() { return fileNames.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public View instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); Glide.with(context).load(fileNames.get(position)).centerCrop().into(imageView); container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } //解决数据不刷新的问题 @Override public int getItemPosition(Object object) { return POSITION_NONE; } }
Demo见 https://github.com/huanyi0723/TestViewPager1
第二个问题 解决刷新单个页面的方法 对于控件采取setTag和findViewWithTag寻找到 再更新 同时注意position和currentPosition
上代码
public class MainActivity extends Activity { private ViewPager view_pager; //预览照片的分页显示 private ViewPagerAdapter viewPagerAdapter; //分页图片的预览器 private ArrayList<String> fileNames = new ArrayList<String>(); //本地图片路径 private int currentPosition = 0; //当前选择的图片 private Timer timer = new Timer(); // 定时器 private TimerTask timerTask; private int progress = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } // 获取本地所有照片路径 private void initData() { fileNames.clear(); Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //图片的保存位置的数据 fileNames.add(new String(data, 0, data.length - 1)); } } //初始化控件 private void initView() { view_pager = (ViewPager) findViewById(R.id.view_pager); viewPagerAdapter = new ViewPagerAdapter(this, fileNames, view_pager, currentPosition); view_pager.setAdapter(viewPagerAdapter); view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int i) { currentPosition = i; Log.i("mbk", "翻页后的当前页面" + currentPosition); } @Override public void onPageScrolled(int i, float f, int j) { } @Override public void onPageScrollStateChanged(int i) { } }); Button delete = (Button)findViewById(R.id.delete); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (fileNames.size() > 0) { fileNames.remove(currentPosition); } Log.i("mbk", "删除的当前页面" + currentPosition); viewPagerAdapter.notifyDataSetChanged(); } }); Button update = (Button)findViewById(R.id.update); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initTimeTask(); } }); } // 定时器轮询 private void initTimeTask() { timerTask = new TimerTask() { @Override public void run() { viewPagerAdapter.setProgress( progress , currentPosition); Log.i("mbk", "---------------MainActivity -----------progress--------------" + progress); progress++; } }; timer.schedule(timerTask, 0, 1000); } }
public class ViewPagerAdapter extends PagerAdapter { private Context context; private ArrayList<String> fileNames = new ArrayList<String>(); // 本地图片路径 private SeekBar seekBar; private ViewPager view_pager; public ViewPagerAdapter(Context context, ArrayList<String> fileNames, ViewPager view_pager ) { super(); this.context = context; this.fileNames = fileNames; this.view_pager = view_pager; } @Override public int getCount() { return fileNames.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public View instantiateItem(ViewGroup container, int position) { View view = LayoutInflater.from(context).inflate(R.layout.pager_adapter_item, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageView); seekBar = (SeekBar) view.findViewById(R.id.seekBar); seekBar.setMax(100); seekBar.setTag("seekBar" + position); Log.i("mbk", "---------------ViewPagerAdapter -----------setTag--------------" + position); Glide.with(context).load(fileNames.get(position)).centerCrop().into(imageView); container.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } // 解决数据不刷新的问题 @Override public int getItemPosition(Object object) { return POSITION_NONE; } public void setProgress(int progress, int currentPosition) { seekBar = (SeekBar) view_pager.findViewWithTag("seekBar"+currentPosition); seekBar.setProgress(progress); Log.i("mbk", "---------------ViewPagerAdapter -----------progress--------------" + progress); } }
Demo见 https://github.com/huanyi0723/TestViewPager2