【问题标题】:Android CoordinatorLayout, Scrolling Activity template all layout is flying awayAndroid CoordinatorLayout, Scrolling Activity 模板所有布局都飞走了
【发布时间】:2016-10-02 01:06:54
【问题描述】:

TL;DR
我希望能够像在 Android Studio 中一样通过 Appbar 本身的手势来隐藏/显示 Appbar 内容 Scrolling Activity 模板。但是需要防止下面描述的奇怪效果。

详情
1.打开Android Studio 2.0,启动新的Android Studio项目,命名为ScrollingActivity,选择Phone and Tablet,API 9.选择Scrolling Activity模板。保留所有其余默认值。
2.在 Android Lollipop 5.1.1 上运行 Activity。就我而言,它是 MS VS 模拟器。
3.将文本滚动到最顶部,以便 Appbar 完全展开(并且 FAB 可见)。

4.注意现在您可以通过应用在 Appbar 本身上的手势滚动布局,展开到全高 180dp 或仅折叠到工具栏。

酷。我想在我的应用中使用它。

5.但是在 Appbar 上使用手势我已经设法使 所有布局,但 FAB 完全消失 :( 奇怪。 好吧,它会在一分钟内恢复,但我绝对不希望它出现在我的应用程序中。

为了获得更多细节,让我们在 Appbar 中添加一个监听器

    // Track how AppBarLayout is flying
    AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            Log.d ("OffsetChangedListener", "appBarLayout goes to=" + verticalOffset );
        }
    });

在我的 XXHDPI 设备上,通常偏移量在 (-273, 0) 范围内。但是突然在Appbar上我的logcat显示了一些 那个Appbar去外太空长途旅行:

06-02 11:37:13.059 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-273
06-02 11:37:15.523 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-186
06-02 11:37:15.529 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=311
06-02 11:37:15.563 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=3833
06-02 11:37:15.564 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0
06-02 11:37:15.638 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=11594
06-02 11:37:15.690 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=16968
06-02 11:37:15.710 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=19137
...
06-02 11:38:07.242 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=2684733
06-02 11:38:07.343 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-536483
...
06-02 11:38:29.983 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-796
06-02 11:38:30.066 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-563
06-02 11:38:30.125 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-422
06-02 11:38:30.202 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-268
06-02 11:38:30.253 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-185
06-02 11:38:30.385 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=-41
06-02 11:38:30.587 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0
06-02 11:38:30.588 18116-18116/com.example.scrollingactivity D/OffsetChangedListener: appBarLayout goes to=0

如何防止这种行为?

【问题讨论】:

    标签: android android-studio android-coordinatorlayout


    【解决方案1】:

    真的不知道这是否是一个答案,但这个解决方法对我有用

    1. AppBarLayout 之后添加虚拟不可滚动视图,使活动布局看起来像

      android:background="#00FF00">
      
      <android.support.design.widget.AppBarLayout
          android:id="@+id/app_bar"
          android:layout_width="match_parent"
          android:layout_height="@dimen/app_bar_height"
          android:fitsSystemWindows="true"
          android:theme="@style/AppTheme.AppBarOverlay">
      
          <android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/toolbar_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fitsSystemWindows="true"
              app:contentScrim="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|exitUntilCollapsed">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  app:layout_collapseMode="pin"
                  app:popupTheme="@style/AppTheme.PopupOverlay" />
      
          </android.support.design.widget.CollapsingToolbarLayout>
      </android.support.design.widget.AppBarLayout>
      
      <!-- Workaround view-->
      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="They'll be back"
          android:id="@+id/dummy"/>
      
      <include layout="@layout/content_scrolling" />
      
      <android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/fab_margin"
          android:src="@android:drawable/ic_dialog_email"
          app:layout_anchor="@id/app_bar"
          app:layout_anchorGravity="bottom|end" />
      

    2. 在appbar的OnOffsetChangedListener添加一些view tackling

    公共类 ScrollingActivity 扩展 AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    
        // Track how AppBarLayout is flying
        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        final View vDummy = findViewById(R.id.dummy);
        final int maxHeight = appBarLayout.getLayoutParams().height; //was set explicitly in XML
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.d("OCL", "appBarLayout goes to=" + verticalOffset);
                vDummy.setVisibility(
                              (verticalOffset >=-maxHeight && verticalOffset<=0)
                              ? View.GONE
                              : View.VISIBLE
                );
            }
        });
    
    }
    

    我仍然可以将 AppBar 发射到外太空,但它很快就会返回

    06-06 17:46:06.510 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=0
    06-06 17:46:06.558 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=0
    06-06 17:46:07.525 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-20
    06-06 17:46:07.562 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-57
    06-06 17:46:07.612 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-114
    06-06 17:46:07.641 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-39239
    06-06 17:46:07.641 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.668 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-81278
    06-06 17:46:07.669 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.703 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-133824
    06-06 17:46:07.704 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.731 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-174358
    06-06 17:46:07.731 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.865 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-377007
    06-06 17:46:07.866 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.885 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-407026
    06-06 17:46:07.886 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-202
    06-06 17:46:07.921 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-162
    06-06 17:46:07.923 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-161
    06-06 17:46:07.923 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-161
    06-06 17:46:07.969 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-104
    06-06 17:46:08.015 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-61
    06-06 17:46:08.067 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-29
    06-06 17:46:08.070 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-29
    06-06 17:46:08.105 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=-12
    06-06 17:46:08.256 13077-13077/com.example.scrollingactivity D/OCL: appBarLayout goes to=0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      相关资源
      最近更新 更多