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 imgs) {
            this.imgFile = imgs;
            notifyDataSetChanged();
        }

        /**刷新视图**/
        public void setList(List 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>

 

转载于:https://www.cnblogs.com/niray/p/4030673.html

你可能感兴趣的:(移动开发)