经过多一点实验/研究后,我从这篇文章中意识到 How to make custom CoordinatorLayout.Behavior with parallax scrolling effect for google MapView?我的问题的很大一部分并不了解视差效应,它翻译视图而不是缩小它们。一旦我意识到,这是微不足道的创建将适用视差我的主要观点自定义的行为,当下片扩展:
public class CollapseBehavior<V extends ViewGroup> extends CoordinatorLayout.Behavior<V>{
public CollapseBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
if (isBottomSheet(dependency)) {
BottomSheetBehavior behavior = ((BottomSheetBehavior) ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior());
int peekHeight = behavior.getPeekHeight();
// The default peek height is -1, which
// gets resolved to a 16:9 ratio with the parent
final int actualPeek = peekHeight >= 0 ? peekHeight : (int) (((parent.getHeight() * 1.0)/(16.0)) * 9.0);
if (dependency.getTop() >= actualPeek) {
// Only perform translations when the
// view is between "hidden" and "collapsed" states
final int dy = dependency.getTop() - parent.getHeight();
ViewCompat.setTranslationY(child, dy/2);
return true;
}
}
return false;
}
private static boolean isBottomSheet(@NonNull View view) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof CoordinatorLayout.LayoutParams) {
return ((CoordinatorLayout.LayoutParams) lp)
.getBehavior() instanceof BottomSheetBehavior;
}
return false;
}
}
然后在我的布局XML,我把我的主视图的app:layout_behavior
到将com.mypackage.CollapseBehavior
和app:layout_anchor
设置为我的底部工作表视图,以便触发onDependentViewChanged
回调。这种效果比尝试调整视图更平滑。我怀疑返回到使用BottomSheetBehavior.BottomSheetCallback
的初始策略也可以类似于此解决方案。
编辑:每个请求,相关的XML在下面。我在运行时将MapFragment
添加到@+id/map_container
中,但这应该也适用于像静态图像那样放入该容器的任何内容。该LocationListFragment
同样可以与任何视图或片段取代,只要它仍然有BottomSheetBehavior
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_coordinator">
<FrameLayout
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:layout_anchor="@+id/list_container"
app:layout_behavior="com.mypackage.behavior.CollapseBehavior"/>
<fragment
android:name="com.mypackage.fragment.LocationListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_container"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
</android.support.design.widget.CoordinatorLayout>
一些[库](https://github.com/wasabeef/awesome-android-ui),它可以提供帮助。 –