【问题标题】:AppBarLayout collapse detection doesn't work properlyAppBarLayout 折叠检测无法正常工作
【发布时间】:2016-07-29 08:32:11
【问题描述】:

我已经按照 GitHub 上的 this 教程设计了一个自定义 AppBarLayout 并检测其完全折叠和完全展开。我将在下面详细介绍 Java & XML 代码

content_main.xml

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true">


    <tries.auro.tryapp.CustomAppBarLayout
        android:id="@+id/appBarLayout"
        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/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="60dp"
                app:layout_collapseMode="pin"
                android:padding="0dp">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/white"
                    android:scaleType="fitXY"
                    android:visibility="gone"/>

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



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

    </tries.auro.tryapp.CustomAppBarLayout>

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

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

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/pic"
                android:scaleType="fitXY"/>

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


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

MainActivity.java

package tries.auro.tryapp;

import android.provider.Settings;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private CustomAppBarLayout appBarLayout;
    private CollapsingToolbarLayout collapsingToolbarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        setOnClickListeners();
    }


    private void init() {

        imageView = (ImageView) findViewById(R.id.imageView);
        appBarLayout = (CustomAppBarLayout) findViewById(R.id.appBarLayout);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
    }

    public void setOnClickListeners() {
        appBarLayout.setOnStateChangeListener(new CustomAppBarLayout.OnStateChangeListener() {
            @Override
            public void onStateChange(CustomAppBarLayout.State toolbarChange) {
                switch (toolbarChange) {
                    case COLLAPSED:
                        System.out.println("***COLLAPSED***");
                    case EXPANDED:
                        System.out.println("***EXPANDED***");
                }
            }
        });
    }
}

CustomAppBarLayout.java

package tries.auro.tryapp;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;

/**
 * Created by stpl on 29/7/16.
 */
public class CustomAppBarLayout extends AppBarLayout implements AppBarLayout.OnOffsetChangedListener {

    public enum State{
        COLLAPSED,
        EXPANDED,
        IDLE
    }

    public interface OnStateChangeListener{
        void onStateChange(State toolbarChange);
    }

    private State state;
    private OnStateChangeListener onStateChangeListener;

    public CustomAppBarLayout(Context context){
        super(context);
    }

    public CustomAppBarLayout(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(!(getLayoutParams() instanceof CoordinatorLayout.LayoutParams)
                || !(getParent() instanceof CoordinatorLayout)){
            throw new IllegalStateException(
                    "CustomAppBarLayout must be a direct child of CoordinatorLayout");
            }
        addOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if(verticalOffset == 0){
            if(onStateChangeListener != null && state != State.EXPANDED){
                onStateChangeListener.onStateChange(State.EXPANDED);
            }
            state = State.EXPANDED;

        } else if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){
            if(onStateChangeListener != null && state != State.COLLAPSED){
                onStateChangeListener.onStateChange(State.COLLAPSED);
            }
            state = State.COLLAPSED;
        }

    }

    public void setOnStateChangeListener(OnStateChangeListener listener) {
        this.onStateChangeListener = listener;
    }
}

问题是: System.out.println 在完全展开时在屏幕上执行***EXPANDED*** 消息,但在完全折叠时两者 ***EXPANDED******COLLAPSED***

以下是我在logcat 中得到的内容

07-29 13:50:04.190 32380-32425/tries.auro.tryapp I/OpenGLRenderer: Initialized EGL, version 1.4
07-29 13:50:04.210 32380-32380/tries.auro.tryapp I/System.out: ***EXPANDED***
07-29 13:50:04.967 32380-32380/tries.auro.tryapp I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
07-29 13:50:08.517 32380-32380/tries.auro.tryapp I/System.out: ***COLLAPSED***
07-29 13:50:08.518 32380-32380/tries.auro.tryapp I/System.out: ***EXPANDED***

我哪里错了?感谢您的宝贵时间!

【问题讨论】:

    标签: java android android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    VIsit to This link。这里在 main.java 中添加 activity_detail.xml 而不是 activity_main.xml。希望这篇能解决你的问题。

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 1970-01-01
      • 2018-08-09
      • 2018-05-02
      • 2020-04-23
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多