In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
本篇内容介绍了"Android怎么使用ViewPager实现画廊Gallery效果"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
在手机QQ上切换封面主题板块,以及在某些App Shop中,我们经常看见Gallery效果的影子,但Gallery以及被一起了,这里我们实现使用ViewPager实现Gallery效果
布局文件:
(注意,根布局和ViewPager都要使用android:clipChildren="false",表示当显示内容区域超过控件区域,
是否裁剪内容区域的宽度和高度,以实现遮盖超出区域,默认是true)
代码文件
public class ViewPagerGallery extends Activity { private ViewPager mGalleryViewPager; private LinearLayout mViewPagerContainer; private int pagerWidth = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewpager_gallery); mGalleryViewPager = (ViewPager)findViewById(R.id.gallery_viewpager); mViewPagerContainer = (LinearLayout)findViewById(R.id.gallery_viewpager_layout); pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels*3.0f/5.0f); mGalleryViewPager.measure(0, 0); LayoutParams lp = mGalleryViewPager.getLayoutParams(); if(lp==null) { lp = new LayoutParams(pagerWidth,LayoutParams.MATCH_PARENT); }else{ lp.width = pagerWidth; } mGalleryViewPager.setLayoutParams(lp);//设置页面宽度为屏幕的3/5 mGalleryViewPager.setOffscreenPageLimit(4); //设置ViewPager至多缓存4个Pager页面,防止多次加载 mGalleryViewPager.setPageMargin(50); //设置Pager之间的间距 mGalleryViewPager.setAdapter(new GalleryPagerAdapter()); mGalleryViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { super.onPageScrolled(position, positionOffset, positionOffsetPixels); if (mViewPagerContainer != null) { mViewPagerContainer.invalidate(); //更新超出区域页面,否则会出现页面缓存,导致页面效果不佳 } } }); } private class GalleryPagerAdapter extends PagerAdapter { @Override public int getCount() { return 6; } @Override public boolean isViewFromObject(View view, Object obj) { return view==obj; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView((ImageView)object); } @Override public Object instantiateItem(ViewGroup container, int position) { int resId = getResources().getIdentifier("img0".concat(String.valueOf(position)), "drawable", getPackageName()); ImageView imageView = createImageView(resId); ((ViewPager)container).addView(imageView, position); return imageView; } public ImageView createImageView(int resId) { ImageView iv = new ImageView(ViewPagerGallery.this); LayoutParams lp = iv.getLayoutParams(); if(lp==null) { lp = new LayoutParams(pagerWidth, LayoutParams.MATCH_PARENT); }else{ lp.width = pagerWidth; lp.height = LayoutParams.MATCH_PARENT; } iv.setScaleType(ScaleType.FIT_CENTER); iv.setImageResource(resId); iv.setLayoutParams(lp); return iv; } }}
Try doing it;
"Android how to use ViewPager to achieve Gallery effect" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.