Android Viewpager作为图像幻灯片库

时间:2012-12-10 06:59:43

标签: android actionbarsherlock android-viewpager

我正在使用杰克的ViewPageIndicator并希望像滑动图库一样显示图像。 我可以开始的任何推荐链接。我已经实现了基本的viewpager,现在想要实现图像视图,如下所示

enter image description here

是否可以使用ViewPageIndicator来实现?

9 个答案:

答案 0 :(得分:72)

在Jake的ViewPageIndicator中,他实现了View pager来显示一个String数组(即 从YourAdapter.java(将FragmentPagerAdapter扩展)传递给YourFragment.java的["this","is","a","text"]),它将View返回给viewpager。

为了显示不同的内容,您只需更改传递的上下文类型即可。在这种情况下,您希望传递图像而不是文本,如下面的示例所示:

这是您设置Viewpager的方式:

public class PlaceDetailsFragment extends SherlockFragment {
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public static final String TAG = "detailsFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_place_details,
                container, false);

        mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        mPager = (ViewPager) view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator) mIndicator).setSnap(true);

        mIndicator
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Toast.makeText(PlaceDetailsFragment.this.getActivity(),
                                "Changed to page " + position,
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageScrolled(int position,
                            float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
        return view;
    }

}

your_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip" />

</LinearLayout>

YourAdapter.java

public class PlaceSlidesFragmentAdapter extends FragmentPagerAdapter implements
        IconPagerAdapter {

    private int[] Images = new int[] { R.drawable.photo1, R.drawable.photo2,
            R.drawable.photo3, R.drawable.photo4

    };

    protected static final int[] ICONS = new int[] { R.drawable.marker,
            R.drawable.marker, R.drawable.marker, R.drawable.marker };

    private int mCount = Images.length;

    public PlaceSlidesFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new PlaceSlideFragment(Images[position]);
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public int getIconResId(int index) {
        return ICONS[index % ICONS.length];
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
}

YourFragment.java

//你需要从这里返回文本的图像.//

public final class PlaceSlideFragment extends Fragment {
    int imageResourceId;

    public PlaceSlideFragment(int i) {
        imageResourceId = i;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ImageView image = new ImageView(getActivity());
        image.setImageResource(imageResourceId);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams());

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }
}

您应该从上面的代码中获得这样的View寻呼机。 enter image description here

答案 1 :(得分:42)

我创建了一个名为AndroidImageSlider的库,您可以尝试一下。

enter image description here

答案 2 :(得分:12)

使用ViewPager实施的图像查看器,检查此项目https://github.com/chiuki/android-swipe-image-viewer

请参阅此讨论Swiping images (not layouts) with viewpager

答案 3 :(得分:4)

只需使用此https://gist.github.com/8cbe094bb7a783e37ad1即可查看周围的网页,并使用此http://viewpagerindicator.com/作为指标。这很酷,我正在将它用于画廊。

答案 4 :(得分:3)

Hoho ..应该很容易使用Gallery实现这一点,使用ViewPager要困难得多。但我仍然鼓励使用ViewPager,因为Gallery确实存在很多问题,并且自Android 4.2以来已被弃用。

在PagerAdapter中有一个方法getPageWidth()来控制页面大小,但只有这样你就无法实现目标。

尝试阅读以下2篇文章寻求帮助。

multiple-view-viewpager-options

viewpager-container

答案 5 :(得分:2)

您可以使用自定义图库控件..查看此https://github.com/kilaka/ImageViewZoom 从这个使用galleryTouch类..

答案 6 :(得分:1)

如果您正在寻找使用圆形指示器滑动的简单Android图像,您可以从此处下载完整的代码http://javaant.com/viewpager-with-circle-indicator-in-android/#.VysQQRV96Hs。请查看现场演示,这将给出清晰的想法。

答案 7 :(得分:0)

enter code here   public Timer timer;
public TimerTask task;
public ImageView slidingimage;

private int[] IMAGE_IDS = {
        R.drawable.home_banner1, R.drawable.home_banner2, R.drawable.home_banner3
    };

enter code here   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

     final Handler mHandler = new Handler();

        // Create runnable for posting
        final Runnable mUpdateResults = new Runnable() {
            public void run() {

                AnimateandSlideShow();

            }
        };
        int delay = 2000; // delay for 1 sec.

        int period = 2000; // repeat every 4 sec.

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

             mHandler.post(mUpdateResults);

        }

        }, delay, period);

enter code here     private void AnimateandSlideShow() {

        slidingimage = (ImageView)findViewById(R.id.banner);
        slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

        currentimageindex++;

        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);

          slidingimage.startAnimation(rotateimage);

    }

答案 8 :(得分:0)

一个很棒的One Image滑块:https://github.com/daimajia/AndroidImageSlider检查