如何在 android 中的 header 上设置图像滑块

How to set image slider on header in android

我是 android 开发新手,请帮助我。 我想在页面顶部设置图像滑块,图像是动态的,来自网络服务。如下所示的图像滑块示例:-

就像这张图片中的滑块显示一样(Back Story shop fro Backpacks)我也想要并记住图片是动态的。

我的activity.main.xml:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/linear_layout_outer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    </LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@+id/linear_layout_outer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            custom:pager_animation="Accordion"
            custom:auto_cycle="true"
            custom:indicator_visibility="visible"
            custom:pager_animation_span="1100" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <HorizontalScrollView
                android:id="@+id/linear_layout_special_product_horizontal_scroll_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/linear_layout_special_produce_image01"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shop240x320"
                            android:paddingRight="65dp"
                            android:contentDescription="@string/special_producr"/>

                        <ImageView
                            android:id="@+id/linear_layout_special_produce_image02"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shop240x320"
                            android:paddingRight="65dp"
                            android:contentDescription="@string/special_producr"/>

                        <ImageView
                            android:id="@+id/linear_layout_special_produce_image03"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shop240x320"
                            android:paddingRight="65dp"
                            android:contentDescription="@string/special_producr"/>

                        <ImageView
                            android:id="@+id/linear_layout_special_produce_image04"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shop240x320"
                            android:paddingRight="65dp"
                            android:contentDescription="@string/special_producr"/>

                        <ImageView
                            android:id="@+id/linear_layout_special_produce_image05"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/shop240x320"
                            android:paddingRight="65dp"
                            android:contentDescription="@string/special_producr"/>
                    </LinearLayout>
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>  
    </LinearLayout>
</ScrollView></RelativeLayout>

目前显示如下:-

这也给出了错误 Failed to find style 'SliderStyle' in current theme what is the meaning

有很多方法可以实现您想要的效果: 1. 通过使用 View Pager。 2. 使用 Recycler View(并设置 Layout Manager Orientation HORIZONTAL)。

以上两个是用于以上述设计格式呈现图像视图(或其他视图)的最简单和基本的小部件。

现在您还需要做一件事:在这些图像视图中呈现您的实际图像。 我假设您正在从某些网络服务中获取 Url 的列表。现在您需要将那些 url 渲染到您的 ImageView 中。为此,您可以使用第三方库,如 picaso 或 Aquery。

关键部分是在附加到这些小部件的适配器中,首先将 Url 的列表传递给该适配器,然后在这些库之一的帮助下设置图像视图源。

与其尝试自己制作,不如使用漂亮而简单的库。 Here就是其中之一。

使用这个库 https://github.com/MoeidHeidari/banner

实施'com.github.MoeidHeidari:banner:1.04'

在你的activity

BannerLayout banner=(BannerLayout) findViewById(R.id.Banner);

    List<String> urls = new ArrayList<>();
    urls.add("http://szzljy.com/images/mountain/mountain4.jpg");
    urls.add("http://szzljy.com/images/mountain/mountain4.jpg");
    urls.add("http://www.visitgreece.gr/deployedFiles/StaticFiles/Photos/Generic%20Contents/Forests/mountains_2_560.jpg");
    urls.add("http://szzljy.com/images/mountain/mountain4.jpg");
    urls.add("http://www.visitgreece.gr/deployedFiles/StaticFiles/Photos/Generic%20Contents/Forests/mountains_2_560.jpg");
    urls.add("http://szzljy.com/images/mountain/mountain4.jpg");


    BaseBannerAdapter webBannerAdapter=new BaseBannerAdapter(this,urls);
    webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
        @Override
        public void onItemClick(int position)
        {


        }
    });
    banner.setAdapter(webBannerAdapter);