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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。