android Whatsapp就像个人资料界面

时间:2015-08-20 02:09:43

标签: android android-recyclerview whatsapp android-cardview android-collapsingtoolbarlayout

我正在寻找一种创建whatsapp的配置文件界面的方法:

enter image description here

我在内部使用了折叠工具栏和一个带有内部CardView的recyclerView,但我的界面存在很多问题。

当我scol the recyclerView时,当工具栏折叠时,内部的项目开始滚动,滚动的空间在之前(工具栏打开时)的同一空间中转动,有图像。

折叠工具栏时会出现阴影。

这是代码:

<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f1f1"
    android:fitsSystemWindows="true">

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

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

            <org.test.myapp.objects.SquareImageView_OBJ
                android:id="@+id/avatar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffff336c"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="@android:color/transparent"
            android:clipToPadding="false"
            android:fadeScrollbars="true"
            android:paddingTop="?attr/actionBarSize"
            android:scrollbars="vertical"
            android:scrollbarThumbVertical="@drawable/scrollbar_color"
            />

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

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

发生了这种情况:

enter image description here

3 个答案:

答案 0 :(得分:3)

必须将Recycler视图移到AppBarLayout外部。并尝试这种方式      

<android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/main_content"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="#f1f1f1"
     android:fitsSystemWindows="true">

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

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

        <org.test.myapp.objects.SquareImageView_OBJ
            android:id="@+id/avatar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffff336c"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/anim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent"
        android:clipToPadding="false"
        android:fadeScrollbars="true"
        android:paddingTop="?attr/actionBarSize"
        android:scrollbars="vertical"
        android:scrollbarThumbVertical="@drawable/scrollbar_color"
        />
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:1)

RecyclerView移出AppBarLayout

答案 2 :(得分:0)

您可以像这样使用NestedScrollView和CoordinatorLayout

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/profile_header_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="16dp"
    app:expandedTitleMarginStart="20dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_collapseMode="parallax">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mainView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/profile_header_height"
            android:background="@color/colorPrimary"
            >

            <ImageView
                android:id="@+id/ivAvatar"
                android:layout_width="@dimen/appbar_profile_pic_height_width"
                android:layout_height="@dimen/appbar_profile_pic_height_width"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/profile_header_margin_top"
                android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
                android:contentDescription="@string/userPictureDes"
                android:src="@drawable/ic_user_circle_white" />

            <TextView
                android:id="@+id/text_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/example_user_name"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/ivAvatar"
                android:layout_marginBottom="@dimen/list_text_padding_top"
                android:layout_marginTop="5dp"
                android:textColor="@android:color/white"
                android:textSize="@dimen/profile_name_text_size" />
        </RelativeLayout>
    </FrameLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="top"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    >
    <!-- your view code here -->
</android.support.v4.widget.NestedScrollView>