【问题标题】:CoordinatorLayout Custom Behavior with LinearLayout as child以 LinearLayout 作为子级的 CoordinatorLayout 自定义行为
【发布时间】:2016-05-17 06:33:25
【问题描述】:

我想在 LinearLayout 上实现自定义行为。 这是我的 xml 的结构:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout.../>
<android.support.v4.widget.DrawerLayout.../>
 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_behavior="com.xxx.BottomBarBehavior"
            android:layout_gravity="bottom|center_horizontal".../>
</android.support.design.widget.CoordinatorLayout>

LinearLayout 位于 CoordinatorLayout 内部,位于 DrawerLayout 下方。 显示快餐栏时,LinearLayout 不会向上移动。我只是无法理解它。布局使得 DrawerLayout 包含一个 ViewPager,并且这个 viewPager 由另一个 xml 通过片段填充。而snackbar是由这个fragment的recyclerView中的一个元素生成的。

这就是我的 BottomBarBehavior 自定义行为类的样子:

public class BottomBarBehavior extends CoordinatorLayout.Behavior<LinearLayout> {
public BottomBarBehavior(Context context, AttributeSet attrs) {}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
    return ( dependency instanceof Snackbar.SnackbarLayout ) ||
            ( dependency instanceof DrawerLayout );
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {
    if( dependency instanceof Snackbar.SnackbarLayout ) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
    }
    return true;
}

}

【问题讨论】:

  • 清理并重建项目。您的layoutDependsOnClassCastException 无关。检查是否再次发生这种情况。
  • 这成功了!但是我的行为仍然没有被LinearLayout效仿。
  • 你传递给 Snackbar 的 make() 调用的 CoordinatorLayout 实例是什么?

标签: android android-layout android-coordinatorlayout coordinator-layout


【解决方案1】:

我认为您只是在BottomBarBehavior 的构造函数中缺少super();

【讨论】:

  • 是的,我确实错过了。但即使在放入 super() 之后,它仍然无法正常工作。创建快餐栏时根本不会调用 onDependentViewChanged()。
  • 我的工作实现和你的唯一不同之处:我不检查 DrawerLayout 内部 layoutDependsOn
  • 但这根本不重要。对吗?
  • 嗯.. 我在 CoordinatorLayout 中也有 android:id="@+id/snackbar",然后调用它 Snackbar sb = Snackbar.make(findViewById(R.id.snackbar), "D'oh", Snackbar.LENGTH_LONG);
  • 所以你的 coordinatorLayout 有 id 'snackbar'?
猜你喜欢
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多