我在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***
&amp; 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***
git merge
我哪里错了?谢谢你的时间!!
答案 0 :(得分:0)
VIsit to This link。这里在main.java中添加activity_detail.xml而不是activity_main.xml。我希望这个解决你的问题。