【问题标题】:Collapsing ToolBar Layout with viewpager使用 viewpager 折叠 ToolBar 布局
【发布时间】:2015-11-02 00:05:53
【问题描述】:

我正在将 CollapsingBarLayout 与 viewpager 一起使用,并且 viewpager 的片段具有 listview、gridview。

这是我的代码:

    <?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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bigsteptech.seandroidnativeapp.classes.modules.common.ViewGroupEvent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/TransparentText">

            <FrameLayout
                android:id="@+id/carouselLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:id="@+id/coverImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:gravity="bottom"
                    android:orientation="vertical"
                    android:layout_gravity="bottom"
                    android:padding="@dimen/profile_image_margin"
                    android:background="@drawable/gradient_bg"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/profile_image_margin"
                        android:textSize="@dimen/text_size_xlarge"
                        android:textStyle="bold"
                        android:textColor="@color/white"
                        android:id="@+id/content_title"/>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_marginLeft="@dimen/profile_image_margin"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textColor="@color/white"
                            android:textSize="@dimen/text_size_medium"
                            android:id="@+id/category_title"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:layout_toRightOf="@+id/category_title"
                            android:layout_marginLeft="@dimen/profile_image_margin"
                            android:textColor="@color/white"
                            android:textSize="@dimen/text_size_medium"
                            android:id="@+id/memberCount"/>

                    </RelativeLayout>


                </LinearLayout>

            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:theme="@style/ActionBarThemeOverlay"
                app:popupTheme="@style/ActionBarPopupThemeOverlay"
                android:background="@drawable/gradient_bg"
                app:layout_collapseMode="pin" />

        </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"
        android:layout_gravity="fill_vertical"
        android:isScrollContainer="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/contentInfo"
                android:paddingBottom="48sp"
                android:clipToPadding="false"
                android:orientation="vertical">

                <ProgressBar
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/profile_page_left_right_margin"
                    android:layout_gravity="center"
                    android:id="@+id/progressBar"/>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:background="@color/grey_light">

                    <TextView android:id="@+id/ownerTitle"
                        android:clickable="true"
                        android:focusable="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/keyline_1"
                        android:layout_gravity="center_vertical"
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                        android:textColor="@color/body_text_1" />

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/profile_image_margin"
                    android:padding="10dp">

                    <com.bigsteptech.seandroidnativeapp.Classes.Modules.Common.ViewRelated.ExpandableTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/keyline_1"
                        style="@style/TextBody"
                        android:layout_gravity="center_vertical"
                        android:id="@+id/view_description" />

                </LinearLayout>

                <android.support.design.widget.TabLayout
                    android:id="@+id/slidingTabs"
                    android:layout_width="match_parent"
                    app:tabIndicatorHeight="3dp"
                    app:tabMode="scrollable"
                    android:layout_height="wrap_content"/>

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

                </android.support.v4.view.ViewPager>


            </LinearLayout>


        </RelativeLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        android:id="@+id/joinGroupButton"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_action_new"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

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

具有listview,gridview的片段在折叠工具栏中不向上滚动,我该如何实现这个工作..

请帮助我,非常感谢您...

【问题讨论】:

  • 我想设计如下布局: 1. 折叠工具栏。 2. NestedScrollview 有 2-3 个文本视图。 3. 在这些 textviews 下方,在同一个 nestedscrollvew 中,我有一些带有 Viewpager 的选项卡。 4. 我有带有列表视图和网格视图的片段。 5. 现在滚动无法正常使用这些列表视图和网格视图。我怎样才能实现这个功能,请你帮帮我。
  • 你检查过我的代码吗?我已经实现了你需要的以及更多
  • 不要使用nestedScrollView、RecyclerView或ListView冲突滚动。
  • 这个问题你解决了吗?

标签: android android-viewpager android-collapsingtoolbarlayout


【解决方案1】:

您需要将 ViewPager 放在 AppBar 部分并设置 layout_behavior

<android.support.design.widget.AppBarLayout>>
    <android.support.design.widget.CollapsingToolbarLayout>
     // THIS VIEWS WILL BE COLLAPSED
    </android.support.design.widget.CollapsingToolbarLayout>

    // THIS VIEWS WILL BE PINNED
    <android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="300dp"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>

如果您使用 Fragments 作为 ViewPager 的子项,您还需要为 ViewPager 中的每个 Fragment 设置 layout_behavior

app:layout_behavior="@string/appbar_scrolling_view_behavior"

【讨论】:

  • 非常感谢,上面的代码我看懂了,已经实现了。但我的问题是我需要在 viewpager 选项卡上方放置更多内容,例如一些 textview,然后需要放置 viewpager。所以,我需要把所有这些东西都放在 NestedScrollview 中,以启用折叠效果。我该如何工作,请帮助我.. :)
  • @PrithnirajNicyone 好的,我明白了。这是一个坏主意。您需要一个滚动处理布局 - ScrollView、ListView 等。在此处阅读更多信息。 stackoverflow.com/questions/3495890/…
  • @PrithnirajNicyone 我想我知道如何帮助你。如果您在 ListView 下没有任何项目,则可以使用不可滚动的 ListView,此处更详细:stackoverflow.com/a/24629341/2956344。和developer.android.com/reference/android/view/…一起玩
【解决方案2】:

使用新材料设计支持库的带有标签的折叠工具栏

我使用了新材料设计支持库的官方折叠工具栏功能。

此处折叠视图高度为 256dp,标签高度为 56dp

我做了以下路径

我将图像分成两部分,一部分用于折叠视图,另一部分用于选项卡。

我根据dp to pixel 尺寸用高分辨率drawable xxxhdpi 剪切图像并放入drawable 文件夹,以便它可以调整到所有屏幕尺寸

我有 2000x1246 的图像

顶部图像 256dp= 2000x1024 像素

底部标签图像 56dp= 2000x224 像素

这是source code的完整示例

【讨论】:

    【解决方案3】:

    ListViewGridView 不具备NestedScrolling 功能。这需要使用CollapsingToolbarLayout

    使其工作的最简单方法是将ListViewGridView 更改为RecyclerViewsRecyclerView 实现NestedScrollingChild)。

    【讨论】:

    • 我的要求是,我不能将我的列表视图更改为 Recyclerview .. 你能给我一些提示,我如何使用带有嵌套滚动视图的列表视图。非常感谢.. :)
    • 你是对的,但是有一个解决这个问题的方法我已经在列表视图中实现了一个网格视图,在创建网格视图后计算静态网格视图高度
    • “我的要求就是这样”,你的意思是“我已经这样做了,不想重写代码”:) ?我不会帮你让ListView 实现 NestedScolling,因为我永远不会打扰。这比重写ListViewGridView 花费的时间要长得多。
    • 对不起@Tony,但这听起来是个糟糕的主意...如果我理解正确,您是在强迫您的GridView 显示其中所有项目的完整高度。因此,您正在禁用使 GridView 可用的唯一东西 - 这是重用其 Views
    • 重用的想法还在,我没有贴出代码,因为这不是他想要的
    【解决方案4】:

    我已经做到了,我将把我的代码和使用的库放在一起 首先这是布局文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/attraction_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f6f5">
    
    <RelativeLayout
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="47dp">
    
        <TextView
            android:id="@+id/exp_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="16dp"
            android:text="See &amp; do"
            android:textColor="#1b7bba"
            android:textSize="17sp" />
    
        <RelativeLayout
            android:id="@+id/relmenu"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true">
    
            <ImageView
                android:layout_width="17dp"
                android:layout_height="14dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="17dp"
                android:layout_marginLeft="8dp"
                android:src="@drawable/menu_icon_blue" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:id="@+id/relsearch"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">
    
            <ImageView
                android:layout_width="18dp"
                android:layout_height="18dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="17dp"
    
                android:layout_marginRight="8dp"
                android:src="@drawable/search_icon_blue" />
    
        </RelativeLayout>
    
    </RelativeLayout>
    
    <View
        android:id="@+id/div"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layout_below="@+id/bar"
        android:background="#1b7bba" />
    
    <FrameLayout
        android:id="@+id/layout_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:layout_below="@+id/div">
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:divider="@null"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:listSelector="@android:color/transparent"
            android:scrollbars="none" />
    
        <FrameLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="290dp"
            android:layout_marginTop="0dp"
            android:orientation="vertical">
    
            <FrameLayout
                android:id="@+id/images_header"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:layout_marginBottom="40dp">
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/gallery"
                    android:layout_width="fill_parent"
                    android:layout_height="250dp"
                    android:clickable="true"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:overScrollMode="never" />
    
                <RelativeLayout
                    android:id="@+id/gallery_back"
    
                    android:layout_width="40dp"
                    android:layout_height="60dp"
                    android:layout_gravity="left|center_vertical">
    
                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="52dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:background="@drawable/arrow_back" />
                </RelativeLayout>
    
                <RelativeLayout
                    android:id="@+id/gallery_next"
                    android:layout_width="40dp"
                    android:layout_height="60dp"
                    android:layout_gravity="right|center_vertical">
    
                    <ImageView
    
                        android:layout_width="20dp"
                        android:layout_height="52dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:background="@drawable/arrow_next" />
                </RelativeLayout>
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="-150dp"
                    android:layout_marginTop="170dp"
                    android:background="@drawable/horizontal_gradient" />
    
                <TextView
                    android:id="@+id/pagenum"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginRight="10dp"
                    android:text="3/15"
                    android:textColor="#FFF"
                    android:textSize="14sp" />
    
            </FrameLayout>
    
            <FrameLayout
                android:id="@+id/header_text_layout"
                android:layout_width="match_parent"
    
                android:layout_height="@dimen/min_height_textheader_materiallike"
                android:layout_gravity="bottom"
                android:background="#FFF">
    
                <!--<TextView-->
                <!--android:id="@+id/text_header"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:layout_gravity="center_vertical"-->
                <!--android:layout_marginLeft="70dp"-->
                <!--android:text="ttttt"-->
                <!--android:textColor="@android:color/white"-->
                <!--android:textSize="18sp"-->
    
                <!--android:textStyle="bold" />-->
                <RelativeLayout
                    android:id="@+id/button_header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="83dp"
                        android:layout_marginRight="83dp">
    
                        <RelativeLayout
                            android:id="@+id/photo"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:layout_weight="2.5">
    
                            <ImageView
                                android:layout_width="17dp"
                                android:layout_height="14dp"
                                android:layout_centerInParent="true"
                                android:background="@drawable/photo_blue_icon"
    
                                />
                        </RelativeLayout>
    
                        <RelativeLayout
                            android:id="@+id/video"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="2.5">
    
                            <ImageView
                                android:layout_width="14dp"
                                android:layout_height="16dp"
                                android:layout_centerInParent="true"
                                android:background="@drawable/video_blue_icon" />
                        </RelativeLayout>
    
                        <RelativeLayout
                            android:id="@+id/share"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="2.5">
    
                            <ImageView
                                android:layout_width="15dp"
                                android:layout_height="20dp"
                                android:layout_centerInParent="true"
                                android:background="@drawable/share_blue_icon" />
                        </RelativeLayout>
    
                        <RelativeLayout
                            android:id="@+id/fav"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="2.5">
    
                            <ImageView
                                android:layout_width="20dp"
                                android:layout_height="19dp"
                                android:layout_centerInParent="true"
                                android:background="@drawable/fav_blue_icon" />
                        </RelativeLayout>
    
                    </LinearLayout>
    
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2px"
                        android:layout_alignParentBottom="true"
                        android:background="#d7d7d7" />
                </RelativeLayout>
            </FrameLayout>
    
        </FrameLayout>
    
    </FrameLayout>
    

    在java代码中你应该添加这个

      StikkyHeaderBuilder.ListViewBuilder.stickTo(mListView)
                    .setHeader(R.id.header, (ViewGroup) contentView)
                    .minHeightHeaderDim(R.dimen.min_height_textheader_materiallike)
                    .animator(new ParallaxStikkyAnimator())
                    .attatch_Acitivty(Attractions.this)
                    .castTo("Attractions")
                    .build();
    

    这是使用的library

    但是我已经修改了这个库以按我的意愿工作

    修改是 stcikylistview 构建器中的 add 方法,以了解使用的活动并传递滚动委托我使用它是因为我在 viewpager 中为图像添加了模糊

    她是我的修改

    我将上传库的修改版本 你在这里https://drive.google.com/file/d/0BxdN8PyW5nmHMmFFeFY2aW9zdlk/view?usp=sharing

    【讨论】:

      【解决方案5】:

      我也有这个问题。您需要使用 RecyclerView。现在我知道您必须使用 ListView 和 GridGiew,但您可以在 RecyclerView 中使用它们。

      例如,我将向您展示如何在 RecyclerView 中实现 GridLayout。

      在您的片段布局(您希望在其中实现 GridView)中,添加以下 XML

      <android.support.v7.widget.RecyclerView
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/recyclerviewGRID"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              />
      

      然后在你的片段 java 上,

      在您的片段 onCreateView 上,定义 RecyclerView,

      RecyclerView rv = (RecyclerView) v.findViewById(R.id.recyclerviewGRID);
      setupRecyclerView(rv);
      

      创建方法setupRecyclerView,

       private void setupRecyclerView(RecyclerView recyclerView) {
              recyclerView.setHasFixedSize(true);
      
                  recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
                  mAdapter = new AdapterGridView(getActivity().getApplicationContext(), mItems);
                  recyclerView.setAdapter(mAdapter);
      
          }
      

      您必须首先将 mAdapter 定义为 RecyclerView.Adapter。然后为 RecyclerView 创建适配器。请注意,mItems 是一个包含列表内容的 ArrayList。

      AdapterGridView.java

      public class AdapterGridView extends RecyclerView.Adapter<AdapterGridView.ViewHolder> {
      
          ArrayList<AdItem> mItems;
          Context context, contxt;
      
          public AdapterGridView(Context context, ArrayList<AdItem> mItems) {
              this.context = context;
              this.mItems = mItems;
          }
      
          @Override
          public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      
      //attach your list item layout here, mine in this case is called list_item_grid_item
              View v = LayoutInflater.from(viewGroup.getContext())
                      .inflate(R.layout.list_item_grid_item, viewGroup, false);
              ViewHolder viewHolder = new ViewHolder(v);
              return viewHolder;
          }
      
          @Override
          public void onBindViewHolder(ViewHolder viewHolder, int i) {
      AdItem singleItem = mItems.get(i);
      
      //attach data to your layout as the following manner
      viewHolder.tvspecies.setText(singleItem.getName());
      
          }
      
          @Override
          public int getItemCount() {
      
      //return the number of list items
              return mItems.size();
          }
      
          class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
      
              public ImageView imgThumbnail, avatar;
              public TextView tvspecies, adIDLBL;
      
      
              public ViewHolder(View itemView) {
                  super(itemView);
                  itemView.setClickable(true);
                  itemView.setOnClickListener(this);
      
      //define your list item views, basically the stuff in list_item_grid_item
                  imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
                  tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
      
              }
      
              @Override
              public void onClick(View v) {
                  //handle item click events
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多