toshkinl toshkinl - 2 months ago 19
Android Question

Swipe down to dismiss for a normal View (outside of RecyclerView)

I have a view that should be dismissable with a swipe-down gesture and it is not in a RecyclerView. But it should look like as if it was made with ItemTouchHelper of the RecyclerView.

Answer
Try using SwipeRefreshLayout in you layout




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thinkpalm.sharedpreffframework.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stackFromBottom="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/editext"
                android:minHeight="60dp"
                android:paddingLeft="20dp" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText"
                android:layout_marginTop="40dp"
                android:background="@drawable/editext"
                android:minHeight="60dp"
                android:paddingLeft="20dp" />

            <Button

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText2"
                android:layout_marginTop="40dp"
                android:background="@drawable/button"
                android:minHeight="60dp"
                android:text="@string/submit"
                android:theme="@style/MyButton" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Initalize the SwipeRefreshLayout in java

        private void initSwipeToRefresh() {
            mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
            mSwipeRefreshLayout.setColorSchemeResources(
                    R.color.colorPrimary);
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {


                }
            });
        }