如何在可折叠工具栏中添加多个图像?

时间:2017-08-16 04:21:21

标签: android xml material-design

enter image description here如何在可折叠工具栏中添加许多图像?我基本上想在可折叠工具栏中显示图像幻灯片。

在下面给出的图像中,灰色区域实际上位于可折叠工具栏内,它表示图像加载器(我称之为幻灯片放映)。现在,当用户向上滚动时,我希望该灰色框与折叠工具栏本身一起消失。那么我如何使该灰色区域包含幻灯片图像

1 个答案:

答案 0 :(得分:2)

这是一个建议。你可以找到Android Image Slider库。

创建布局结构:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/spiceCoordinatorLayoutId"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayoutId"
    android:layout_width="match_parent"
    android:layout_height="290dp"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/colorNavigationBarHeaderColor"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:statusBarScrim="@color/colorAreaStatusBarColor"
        app:titleEnabled="false">

        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/spiceToolbarId"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:navigationIcon="?attr/homeAsUpIndicator">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

  </android.support.design.widget.AppBarLayout>

  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/llSpiceDetailContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:orientation="vertical">

       // some content           

    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

此代码来自MainActiivty库的演示活动Android Image Slider

  

https://github.com/daimajia/AndroidImageSlider/blob/master/demo/src/main/java/com/daimajia/slider/demo/MainActivity.java

private SliderLayout mDemoSlider;

mDemoSlider = (SliderLayout)findViewById(R.id.slider);

    HashMap<String,String> url_maps = new HashMap<String, String>();
    url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
    url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
    url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
    url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

    HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
    file_maps.put("Hannibal",R.drawable.hannibal);
    file_maps.put("Big Bang Theory",R.drawable.bigbang);
    file_maps.put("House of Cards",R.drawable.house);
    file_maps.put("Game of Thrones", R.drawable.game_of_thrones);

    for(String name : file_maps.keySet()){
        TextSliderView textSliderView = new TextSliderView(this);
        // initialize a SliderLayout
        textSliderView
                .description(name)
                .image(file_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);

        //add your extra information
        textSliderView.bundle(new Bundle());
        textSliderView.getBundle()
                    .putString("extra",name);

       mDemoSlider.addSlider(textSliderView);
    }