Dragger Android Library

repository·master·Indexed 23 days ago

https://github.com/ppamorim/dragger

An Android library for creating smooth, realistic activity transitions and draggable views using ViewDragHelper and Facebook's Rebound. It provides DraggerActivity and DraggerView components to implement high-performance panels and drawers with customizable tension, friction, and anchor positions.

Tokens
1.6K
Snippets
5
Records
6
Agent score
30%

What's inside Dragger

  1. Install Dragger via JitPack

    master

    Add the JitPack repository to your build.gradle file and then include the Dragger dependency in your dependencies block.

    repositories {
      maven {
        url "https://jitpack.io"
      }
    }
    
    dependencies {
      implementation 'com.github.ppamorim:dragger:1.2'
    }
  2. Integrate Dragger with Scrollable Lists (RecyclerView/ScrollView)

    master

    To prevent conflicts between Dragger and scrollable lists, use Android-ObservableScrollView to detect scroll position. You can then use draggerView.setSlideEnabled(scrollY != 0) to disable Dragger's drag functionality while the user is actively scrolling the list.

    public void configRecyclerView() {
      ... 
      recyclerView.setScrollViewCallbacks(onObservableScrollViewCallbacks);
    }
    
    private ObservableScrollViewCallbacks onObservableScrollViewCallbacks =
        new ObservableScrollViewCallbacks() {
      @Override public void onScrollChanged(int scrollY, boolean firstScroll, 
          boolean dragging) {
        draggerView.setSlideEnabled(scrollY != 0);
      }
      @Override public void onDownMotionEvent() { }
      @Override public void onUpOrCancelMotionEvent(ScrollState scrollState) { }
    };
  3. Configure Activity Theme for Dragger

    master

    Dragger requires a translucent theme to work correctly. Ensure your activity theme in styles.xml includes android:windowIsTranslucent set to true and android:windowBackground set to transparent. You should also disable the window animation style.

    <style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>
  4. Configure DraggerView in XML layout

    master

    To use Dragger as a view, add DraggerView to your root layout. It requires two child layouts: a shadow view (which should be invisible) and a drag view. Use the dragger_layout namespace to specify their IDs and the anchor position.

    Required attributes:

    • dragger_layout:drag_view_id: The ID of the view that will be dragged.
    • dragger_layout:shadow_view_id: The ID of the shadow view.
    • dragger_layout:drag_position: The anchor position (e.g., top).
    <com.github.ppamorim.library.DraggerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        dragger_layout:drag_view_id="@+id/drag_view"
        dragger_layout:shadow_view_id="@+id/shadow_view"
        dragger_layout:drag_position="top">
    
        <FrameLayout
              android:id="@+id/shadow_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/transparent"
              android:visibility="invisible"/>
    
        <LinearLayout
              android:id="@+id/drag_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
    
    </com.github.ppamorim.library.DraggerView>
  5. Implement DraggerActivity for fast performance

    master

    For better performance, extend DraggerActivity instead of using DraggerView in a standard layout. You can optionally call setShadowView(int layoutResId) to define a custom shadow layout before calling setContentView().

    public class YourActivity extends DraggerActivity {
      @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setShadowView(R.layout.custom_shadow); // It is not necessary, use if you want.
        setContentView(R.layout.layout_content);
      }
    }
  6. DraggerActivity Methods Reference

    master

    Use these methods to control the behavior and animation of the Dragger component:

    • setDraggerCallback(DraggerCallback): Provides information about the animation lifecycle via an interface.
    • setSlideEnabled(boolean): Enables or disables dragging. Useful for preventing conflicts with ScrollView or RecyclerView.
    • setHorizontalDragRange(float): Sets the horizontal draggable distance.
    • setVerticalDragRange(float): Sets the vertical draggable distance.
    • setRunAnimationOnFinishInflate(boolean): If true, runs the initial animation upon inflation.
    • setDraggerLimit(float): Sets the maximum drag limit (default is 0.5, the center of the screen).
    • setDraggerPosition(DraggerPosition): Sets the anchor position.
    • setTension(float): Sets the animation tension (friction/duration).
    • setFriction(float): Sets the friction applied during the tension animation.
    • show(): Displays the drag view using Rebound animations.
    • closeActivity(): Closes the activity using a Rebound animation based on the chosen DraggerPosition.
    • expand(): Controls the slide of the view.