5

mein Versuch, die „NEXT“ Taste nach oben zu drücken, wenn die snackbar sichtbar ist:Wie schiebe ich eine vorhandene Ansicht, wenn Snackbar angezeigt wird? Dies ist

Wie Sie sehen können, ist es nicht wie geplant funktionieren. Es scheint, als ob die textview wurde nach oben und hinter der RelativeLayout geschoben und erscheint dann wieder, wenn die snackbar verschwindet. Stattdessen möchte ich, dass die textview hochgeschoben erscheint (also über der snackbar liegt) und dann wieder herunterfährt, wenn die snackbar verschwindet. Ich habe auch ein kleines GitHub Repo demonstrieren diese erstellt:

https://github.com/Winghin2517/TestFabMotion

Hier ist die CoordinatorLayout.Behavior ich für den „NEXT“ verwenden, um mich zu verstecken. Das Verhalten wird von dem FAB Verhalten genommen, wenn es nach oben gedrückt wird, wenn die snackbar erscheint:

public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<TextView> { 

    public FloatingActionButtonBehavior(Context context, AttributeSet attrs) { 
    } 

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) { 

     return dependency instanceof Snackbar.SnackbarLayout; 
    } 

    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) { 

     float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()); 
     child.setTranslationY(translationY); 
     return true; 
    } 
} 

Ich habe versucht, dies dieses xml mit:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Click Here!" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:background="@color/colorAccent" 
      android:id="@+id/click_here" 
      android:textSize="22sp" 
      android:layout_marginBottom="12dp" 
      android:gravity="center_horizontal" 
      android:paddingTop="24dp" 
      android:paddingLeft="24dp" 
      android:paddingRight="24dp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/click_here" 
      android:layout_centerHorizontal="true" 
      android:src="@mipmap/ic_launcher"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/next" 
      android:text="Next" 
      android:layout_alignParentBottom="true" 
      android:background="@color/colorAccent" 
      android:clickable="true" 
      android:gravity="center_horizontal" 
      android:padding="16dp" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" /> 

    </RelativeLayout> 

    </android.support.design.widget.CoordinatorLayout> 

</LinearLayout> 

ich auch versucht habe, mit diesem xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Click Here!" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:background="@color/colorAccent" 
      android:id="@+id/click_here" 
      android:textSize="22sp" 
      android:layout_marginBottom="12dp" 
      android:gravity="center_horizontal" 
      android:paddingTop="24dp" 
      android:paddingLeft="24dp" 
      android:paddingRight="24dp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/click_here" 
      android:layout_centerHorizontal="true" 
      android:src="@mipmap/ic_launcher"/> 

    </RelativeLayout> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorlayout" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/next" 
      android:text="Next" 
      android:background="@color/colorAccent" 
      android:clickable="true" 
      android:gravity="center_horizontal" 
      android:padding="16dp" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" /> 

    </android.support.design.widget.CoordinatorLayout> 

</LinearLayout> 

Es scheint einfach nicht zu funktionieren. Weiß jemand, wie ich es zum Laufen bringen kann?

Antwort

1

Nach mehreren Lösungen zu testen, stieß ich auf eine xml Layout, das funktioniert.

Der wesentliche Unterschied zwischen dem, was ich vorher versucht hatte, und die unten ist, dass die coordinatorlayout Höhe „match_parent“ ist, was bedeutet es, die Ansichten darunter überlagert, sondern weil die coordinatorlayout transparent ist, Sie sehen es nicht, und wenn die snackbar drückt von unten nach oben, wird es genug Platz hat nun die textview über den snackbar drücken:

<?xml version="1.0" encoding="utf-8"?> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorPrimary" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_weight="1"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Click Here!" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:background="@color/colorAccent" 
      android:id="@+id/click_here" 
      android:textSize="22sp" 
      android:layout_marginBottom="12dp" 
      android:gravity="center_horizontal" 
      android:paddingTop="24dp" 
      android:paddingLeft="24dp" 
      android:paddingRight="24dp" /> 

     <ImageButton 
      android:id="@+id/btnComments" 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:layout_below="@+id/click_here" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/shadow_bg" 
      android:src="@mipmap/ic_launcher" /> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|bottom"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/next" 
      android:text="Next" 
      android:background="@color/colorAccent" 
      android:clickable="true" 
      android:gravity="center_horizontal" 
      android:padding="16dp" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:layout_gravity="center_horizontal|bottom" 
      app:layout_behavior="fabmotiontest.com.fabtest.FloatingActionButtonBehavior" /> 

    </android.support.design.widget.CoordinatorLayout> 

</RelativeLayout> 
+0

Hallo zusammen, simon, in meinem Fall muss ich Wrap Inhalt auf cardView setzen, um es unten zu zeigen, aber wenn die Snackbar zeigt, dass cardview hochgeht, wird es wegen des Wrap Contents geschnitten, aber wenn man es auf Eltern setzt, wird es auf die Oberseite gesetzt alle anzeigen. bitte vorschlagen.! RL -> Cordinator -> CardView (mit Fab-Eigenschaft) -> Relative Layout (mit zwei Bildern) –

0

Was ist das:

Button button = (Button)findViewById(R.id.next); 
button.setLayoutParams(new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams, 
    myNewX, myNewY)); 

Ich weiß nicht, welche x und y-Koordinaten zu verwenden, werden Sie versuchen müssen ...

+1

Dies wird nicht animiert die Taste. Dies wird nur die Größe ändern. – Simon

Verwandte Themen