首页 > 代码库 > Android - ViewPager实现Gallery效果
Android - ViewPager实现Gallery效果
RelativeLayout viewPagerContainer = (RelativeLayout) headerView.findViewById(R.id.content_pager_layout); imgPager = (ViewPagerFixed) headerView.findViewById(R.id.content_VP); imgPager.setPageMargin(10); pagerAdapter = new MyPagerAdapter(); pagerAdapter.setFileList(mImages); imgPager.setAdapter(pagerAdapter); imgPager.setOnPageChangeListener(new MyOnPageChangeListener(viewPagerContainer)); viewPagerContainer.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view. return imgPager.dispatchTouchEvent(event); } });
public class MyOnPageChangeListener implements OnPageChangeListener { private RelativeLayout viewPagerContainer; public MyOnPageChangeListener(RelativeLayout viewPagerContainer) { this.viewPagerContainer = viewPagerContainer; } @Override public void onPageSelected(int position) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (viewPagerContainer != null) { viewPagerContainer.invalidate(); } } @Override public void onPageScrollStateChanged(int arg0) { } }
class MyPagerAdapter extends PagerAdapter {/**刷新视图**/ public void setFileList(List<File> imgs) { this.imgFile = imgs; notifyDataSetChanged(); } /**刷新视图**/ public void setList(List<img> row) { this.imgList = row; notifyDataSetChanged(); } @Override public int getCount() { return size; } @Override public boolean isViewFromObject(View view, Object object) { return (view == object); } @Override public Object instantiateItem(ViewGroup container, int position) { final ImageView imageView = new ImageView(mActivity); imageView.setBackgroundColor(Color.LTGRAY); imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); imageView.setTag(imageUrl); imageView.setImageBitmap(loadedImage); ((ViewPager) container).addView(imageView, 0); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((ImageView) object); } }
<RelativeLayout android:id="@+id/content_pager_layout" android:layout_width="match_parent" android:layout_height="365dp" android:clipChildren="false" android:layerType="software" > <com.dotfive.chuanbang.layout.ViewPagerFixed android:id="@+id/content_VP" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="15dp" android:clipChildren="false" android:gravity="center" /> </RelativeLayout>
Android - ViewPager实现Gallery效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。