Fixed edge and exit of Android wear 2.0 app

I am writing a wear android app and I need to achieve the same behavior as google maps app when android is disabled when the app is closed.

Typically, a "Scroll Down" gesture (swipe left to right) will close the application (activity), but you could imagine a situation where you have an image or a map and need to pan the application. Swiping from left to right will always close the application.

There should be a solution as described by Google at https://developer.android.com/training/wearables/ui/exit.html#swipe-to-dismiss .

If you want the user to push down the back stack, you can wrap the view in a SwipeDismissFrameLayout object that supports scrolling. The edge swipe is enabled when the view or its children return true from canScrollHorizontally (). A border stroke allows the user to dismiss the view by scrolling from the left side of the screen (currently set to 10% of the screen width), not just anywhere in the view.

This is implemented in the Google maps app for Android carry. The app closes just in case you start scrolling very close to the left. It's called Edge Stroke.

The problem is that canScrollHorizontally () must return true and I cannot set this view property correctly - it is always false. https://developer.android.com/reference/android/support/wearable/view/SwipeDismissFrameLayout.html

Layout file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.SwipeDismissFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_dismiss_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true"
    android:nestedScrollingEnabled="true"
    tools:context="jhettler.wearmaps.MainActivity"
    tools:deviceIds="wear">

    <RelativeLayout
        android:id="@+id/map_area"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadeScrollbars="false"
        android:isScrollContainer="true"
        android:nestedScrollingEnabled="true">

    </RelativeLayout>

</android.support.wearable.view.SwipeDismissFrameLayout>
      

Run codeHide result


I would not like to use legacy classes like DismissOverlayView and use the power button to exit the application.

Any ideas or examples on how to do this?

Thank!

+3


source to share


4 answers


Try to disable scrolling with this code:

<resources>
  <style name="AppTheme" parent="@android:style/Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
  </style>
</resources> 

      

Disabling scrolling for dismiss is generally not recommended as the user expects to dismiss any screen with a swipe. In an exceptional case, you can set windowSwipeToDismiss to false and the user will press the power button to exit your application.



This can be found in the Behavior Changes document for Android Wear 2.0. Things like the reintroduction of Swipe-to-Dismiss, the power button on the Watch Face, and other changes for Android Wear 2.0 were discussed here.

Hope it helps.

0


source


I think you should extend SwipeDismissFrameLayout and create your own class.

I have no problem with this code.

public class MySwipeDismissFrameLayout extends SwipeDismissFrameLayout {
    public MySwipeDismissFrameLayout(Context context) {
        super(context);
    }

    public MySwipeDismissFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwipeDismissFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, float dx, float x, float y) {
        return true;
    }
}

      



and

<?xml version="1.0" encoding="utf-8"?>
<com.example.MySwipeDismissFrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

      

0


source


Yu Kobayashi's answer is effective if it MySwipeDismissFrameLayout

contains a scrollable view. If there is no scrollable view, you can use onPreSwipeStart .

        new SwipeDismissFrameLayout.Callback() {

            @Override
            public boolean onPreSwipeStart(float xDown, float yDown) {
                Display display = getActivity().getWindowManager().getDefaultDisplay();
                Point displaySize = new Point();
                display.getSize(displaySize);
                return xDown < displaySize.x / 10;
            }
        };

      

0


source


As you noted in the Android developer documentation, to enable edge scrolling, your instance, SwipeDismissFrameLayout

or at least one of its contained views, must return true

when the method is called canScrollHorizontally()

.

However, in your layout file, the only view presented is the stock RelativeLayout

that returns false

from canScrollHorizontally()

.

One way to achieve your goal is to extend View

and then override its method canScrollHorizontally()

, minimally something like this:

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    @Override
    public boolean canScrollHorizontally(int direction) {
        return true;
    }
}

      

and then in your layout file inside SwipeDismissFrameLayout

, put:

    <com.mydomain.MyView
        android:id="@+id/myView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

      

if your package name com.mydomain

.

0


source







All Articles