简体   繁体   English

使用viewpager折叠ToolBar布局

[英]Collapsing ToolBar Layout with viewpager

I am using CollapsingBarLayout with viewpager and the fragments of viewpager are having listview, gridview. 我正在使用带有viewpager的CollapsingBarLayout,并且viewpager的片段具有listview,gridview。

Here is my code: 这是我的代码:

    <?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>

The fragments which are having listview, gridview do not scroll to up in collapsing toolBar, how can I achieve this working.. 具有listview,gridview的片段不会在折叠工具栏中向上滚动,我怎样才能实现这个工作..

Please help me, thank you so much in advanced... 请帮助我,非常感谢先进...

You need put your ViewPager inside AppBar section and setup layout_behavior : 您需要将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>

If you using Fragments as children of ViewPager you need setup layout_behavior to each fragment inside ViewPager too. 如果您使用Fragments作为ViewPager的子级,则还需要为ViewPager中的每个片段设置layout_behavior

app:layout_behavior="@string/appbar_scrolling_view_behavior"

collapsing toolbar with tabs using new material design support library 使用新材料设计支持库折叠工具栏和选项卡

在此输入图像描述

I used official collapsing toolbar feature of new material design support library. 我使用了新材料设计支持库的官方折叠工具栏功能。

here collapsed view height is 256dp and tabs height is 56dp 折叠视图高度为256dp,标签高度为56dp

i made following path 我走了以下路

i cut image into two parts one for collapsed view and one for tabs. 我将图像分为两部分,一部分用于折叠视图,另一部分用于标签。

i cutted images according to dp to pixel sizes with high resolution drawable xxxhdpi and put into drawable folder so it will adjustable to all screen sizes 我根据dp切割图像到像素大小,高分辨率可绘制xxxhdpi并放入可绘制文件夹,因此它可以调整到所有屏幕尺寸

i have 2000x1246 image 我有2000x1246图像

top image 256dp= 2000x1024 pixel 顶部图像256dp = 2000x1024像素

bottom tab image 56dp= 2000x224 pixel 底部标签图像56dp = 2000x224像素

Here is the complete example with source code 这是包含源代码的完整示例

ListView and GridView are not equipped with NestedScrolling functions. ListViewGridView没有配备NestedScrolling函数。 And thats needed to work with the CollapsingToolbarLayout . 这需要使用CollapsingToolbarLayout

The easiest way to make it work, would be to change your ListView and GridView to RecyclerViews ( RecyclerView implements NestedScrollingChild ). 最简单的方法是将ListViewGridView更改为RecyclerViewsRecyclerView实现NestedScrollingChild )。

i have achieved this i will put my code with the library used firstly this is the layout file 我已经实现了这个我将我的代码与首先使用的库,这是布局文件

<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>

and in the java code you should add this 在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();

and here is the library used 这是使用的

but i have modified this library to work as i want 但我已修改此库以按我的意愿工作

the modification is add method in stcikylistview builder to know the activity used and pass a delegate for scrolling i used this because i added a blur for images in the viewpager 修改是在stcikylistview构建器中添加方法以了解所使用的活动并传递一个委托进行滚动我使用了这个因为我在viewpager中为图像添加了模糊

and her is my modification 而她就是我的修改

i will upload the modified version of the library here you are https://drive.google.com/file/d/0BxdN8PyW5nmHMmFFeFY2aW9zdlk/view?usp=sharing 我将在这里上传修改后的库版本,你可以访问https://drive.google.com/file/d/0BxdN8PyW5nmHMmFFeFY2aW9zdlk/view?usp=sharing

I had this issue as well. 我也有这个问题。 You'll need to used RecyclerView. 你需要使用RecyclerView。 Now I understand you must use ListView and GridGiew, but you can use that within a RecyclerView. 现在我明白你必须使用ListView和GridGiew,但你可以在RecyclerView中使用它。

For example, I'll show you how to implement a GridLayout within in a RecyclerView. 例如,我将向您展示如何在RecyclerView中实现GridLayout。

In your fragment layout (that you'd want to implement GridView in), add the following XML 在您的片段布局(您想要实现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"
        />

Then on your fragment java, 然后在你的片段java上,

on your fragment onCreateView, define the RecyclerView, 在你的片段onCreateView上,定义RecyclerView,

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

Create the method setupRecyclerView, 创建方法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);

    }

You must define mAdapter as RecyclerView.Adapter first. 您必须首先将mAdapter定义为RecyclerView.Adapter Then create your the adapter for the RecyclerView. 然后为RecyclerView创建适配器。 Note that, mItems is an ArrayList that will contain contents of your list. 请注意,mItems是一个ArrayList,它将包含列表的内容。

AdapterGridView.java 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
        }
    }
}

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

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