我得到了一個線性佈局,當Snackbar出現時我想向上移動。如何移動Snackbar上方的視圖就像FloatingButton
我看到很多例子如何用FloatingButton做到這一點,但是對於常規視圖呢?
我得到了一個線性佈局,當Snackbar出現時我想向上移動。如何移動Snackbar上方的視圖就像FloatingButton
我看到很多例子如何用FloatingButton做到這一點,但是對於常規視圖呢?
您需要將行爲添加到您的LinearLayout並將其嵌入到CoordinatorLayout中。您可能想要閱讀下面的內容:http://alisonhuang-blog.logdown.com/posts/290009-design-support-library-coordinator-layout-and-behavior
不需要協調員佈局使用常規視圖將小吃棒對準視圖的底部,並將按鈕放置在視圖的頂部,點擊任何您的邏輯顯示小吃店或線性按鈕佈局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
/*snackbar code
<LinearLayout
android:id="@+id/linear_snack bar"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_45"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/margin_0"
android:background="@color/dark_grey">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/orange"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/text_size_h7" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="@dimen/text_size_h7" />
</LinearLayout>
<View android:layout_above="@+id/linear_snack bar"
</RelativeLayout>
我打算詳細說明已批准的答案,因爲我認爲這個實現比本文提供的稍微簡單一些。
我沒能找到一個內置行爲處理意見的通用移動,但是這一次是一個很好的通用選項(從http://alisonhuang-blog.logdown.com/posts/290009-design-support-library-coordinator-layout-and-behavior在另一個鏈接的評論):
import android.content.Context;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
@Keep
public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
public MoveUpwardBehavior() {
super();
}
public MoveUpwardBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, ViewCompat.getTranslationY(dependency) - dependency.getHeight());
ViewCompat.setTranslationY(child, translationY);
return true;
}
//you need this when you swipe the snackbar(thanx to ubuntudroid's comment)
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
ViewCompat.animate(child).translationY(0).start();
}
}
然後在佈局文件中添加如下一個layout_behavior:
<LinearLayout
android:id="@+id/main_content"
android:orientation="vertical"
app:layout_behavior="com.example.MoveUpwardBehavior"/>
其中layout_behavior是你的自定義行爲的完整路徑。除非特別需要具有默認行爲,否則不需要對LinearLayout進行子類化,這似乎並不常見。
這與公認的解決方案有何不同? –
此解決方案不需要繼承目標視圖,我覺得這是過度的。此外,通常最好的做法是直接解釋答案,因爲鏈接會在一段時間後消失。 –
偉大的解決方案。爲了支持小吃棒的輕掃 - 解除,添加下面的代碼:'@Override public void onDependentViewRemoved(CoordinatorLayout parent,View child,View dependency){super.onDependentViewRemoved(parent,child,dependency); child.setTranslationY(0); }' – ubuntudroid
我實現了這一點,發現小吃棒消失後,小吃棒的地方留下了空白,顯然這是衆所周知的,如果動畫已被禁用的設備。
爲了解決這個問題,我改變了onDependentViewChanged方法來存儲這個行爲所附加的視圖的初始Y位置。然後在去除小吃吧的這一觀點的位置復位到存儲的Y位置
private static float initialPositionY;
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
initialPositionY = child.getY();
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
super.onDependentViewRemoved(parent, child, dependency);
child.setTranslationY(initialPositionY);
}
除了特拉維斯卡斯蒂略的回答是: 允許觸發連續小吃店,內onDependentViewChanged()
,必須取消任何可能正在進行的動畫開始通過onDependentViewRemoved()
:
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, ViewCompat.getTranslationY(dependency) - dependency.getHeight());
ViewCompat.animate(child).cancel(); //cancel potential animation started in onDependentViewRemoved()
ViewCompat.setTranslationY(child, translationY);
return true;
}
不取消,當小吃吧被另一個小吃吧取代的LinearLayout會跳下第二小吃吧以下。
基於@Travis卡斯蒂略答案。如修復問題:
Moving entire layout up and cause the objects on top of view disappear.
Doesnt push the layout up when showing SnackBars immediately after eachother.
所以這裏是MoveUpwardBehavior類固定碼:
import android.content.Context;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
@Keep
public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
public MoveUpwardBehavior() {
super();
}
public MoveUpwardBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, ViewCompat.getTranslationY(dependency) - dependency.getHeight());
//Dismiss last SnackBar immediately to prevent from conflict when showing SnackBars immediately after eachother
ViewCompat.animate(child).cancel();
//Move entire child layout up that causes objects on top disappear
ViewCompat.setTranslationY(child, translationY);
//Set top padding to child layout to reappear missing objects
//If you had set padding to child in xml, then you have to set them here by <child.getPaddingLeft(), ...>
child.setPadding(0, -Math.round(translationY), 0, 0);
return true;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
//Reset paddings and translationY to its default
child.setPadding(0, 0, 0, 0);
ViewCompat.animate(child).translationY(0).start();
}
}
這碼推高看到屏幕上的哪些用戶和除用戶在你的佈局,而小吃吧訪問所有對象正在顯示。
如果你想在小吃吧覆蓋的對象,而不是推動,此外用戶也可以訪問所有對象,那麼你需要改變方法onDependentViewChanged:
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, ViewCompat.getTranslationY(dependency) - dependency.getHeight());
//Dismiss last SnackBar immediately to prevent from conflict when showing SnackBars immediately after eachother
ViewCompat.animate(child).cancel();
//Padding from bottom instead pushing top and padding from top.
//If you had set padding to child in xml, then you have to set them here by <child.getPaddingLeft(), ...>
child.setPadding(0, 0, 0, -Math.round(translationY));
return true;
}
和方法onDependentViewRemoved:
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
//Reset paddings and translationY to its default
child.setPadding(0, 0, 0, 0);
}
不幸的是,當用戶滑動刪除SnackBar時,您將丟失動畫。並且您必須使用ValueAnimator類來爲填充更改制作動畫,這會在此處產生一些衝突,您必須對其進行調試。
https://developer.android.com/reference/android/animation/ValueAnimator.html
有關動畫刷卡刪除小吃吧所有評論贊賞。
如果你可以跳過那個動畫,那麼你可以使用它。不管怎麼說,我推薦第一種。
我寫過一個庫,可以通過SnackProgressBar將其他視圖添加到動畫中。它還包括progressBar和其他東西。試試吧https://github.com/tingyik90/snackprogressbar
假設你有以下的動畫視圖。
View[] views = {view1, view2, view3};
在您的活動中創建一個SnackProgressBarManager實例,並將視圖添加到動畫中。
SnackProgressBarManager snackProgressBarManager = new SnackProgressBarManager(rootView)
.setViewsToMove(views)
當一個SnackProgressBar被顯示或消除時,這些視圖將被相應的動畫化。
我不知道你以前是否使用過小吃棒,但這不是一個常見的實現。通常它是在運行時添加的動態警報。如果這是一種爲你工作的技術,那麼試着更具體一些,而不是在「快餐欄代碼」中進行評論,這可以說是佈局中更重要的部分之一。 –