简体   繁体   English

使用ViewPager的图像幻灯片

[英]Image Slideshow using ViewPager

I want to implement the following screen : 我要实现以下屏幕:

截图

In the screen shot you can see that below MyAdvisor TextView i have an image.On swiping this image different image will be displayed .To create Swipe Gallery i am using view pager here.I am using an Adapter which is providing images to display through view pager. 在屏幕快照中,您可以看到MyAdvisor TextView下方有一个图像。在滑动此图像时,将显示不同的图像。要创建Swipe Gallery,我在这里使用View pager。我正在使用提供图像以通过视图显示的适配器寻呼机。

Adapter: 适配器:

   public class ImageAdapter extends PagerAdapter {

    private Context context;
    private int[] images = new int[]{R.drawable.imgone,
            R.drawable.img2,
            R.drawable.img3};

    public ImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (ImageView) object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setImageResource(images[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
}

Below is the xml file of demo project of mine which contains two text view and a view pager. 下面是我的演示项目的xml文件,其中包含两个文本视图和一个视图分页器。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:layout_above="@+id/txt"
    android:layout_marginTop="15sp"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txt"
    android:text="Hello2"
    android:layout_above="@+id/pager"
    android:layout_marginTop="15sp"/>



<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height = "wrap_content"
    ></android.support.v4.view.ViewPager>

My problem is that the view pager is having height as match_parent.Even after changing it as wrap_content ,the other views are not displayed .Please guide me how can i implement this screen. 我的问题是视图寻呼机的高度为match_parent。即使将其更改为wrap_content,其他视图也不会显示。请指导我如何实现此屏幕。

我建议您Heterogenous Layouts inside RecyclerView使用Heterogenous Layouts inside RecyclerView ,如果您不熟悉此概念,请继续阅读本教程 。对于viewpager部分,有一个很棒的提供了很多内置功能,您可以绝对会喜欢。 Sliderlayout在适配器的onCreateViewHolder()中膨胀该库的Sliderlayout并提供相应的数据即可,无需编写自己的自定义pageradapter和处理事件。您只需要知道如何使用Recyclerview就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM