Skip to content

A staggered grid library with drag and drop capabilities for android.

License

Notifications You must be signed in to change notification settings

dbilgin/AndroidDragDropStaggeredGrid

Repository files navigation

AndroidDragDropStaggeredGrid

A two column staggered grid library with drag and drop capabilities for android.

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency

dependencies {
  implementation 'com.github.iatsu:AndroidDragDropStaggeredGrid:0.1.3'
}

A simple guide

First add it to your layout as below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <com.iatsu.dragdropstaggeredgridlibrary.DragDropStaggeredGrid
            android:id="@+id/dragDropStaggeredGrid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

</android.support.constraint.ConstraintLayout>

In your activity, you can implement the Draggable interface, which lets you override dragging() and dragEnded() Below, you will see the basic usages of the methods available in DragDropStaggeredGrid

lateinit var dragDropStaggeredGrid: DragDropStaggeredGrid

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    dragDropStaggeredGrid = findViewById(R.id.dragDropStaggeredGrid)
    //this callback lets you override dragging() and dragEnded()
    dragDropStaggeredGrid.callback = this

    //region add view list to layout
    val textViewList = mutableListOf<View>()
    for (i in 0 until 10) {
        val textView = TextView(this)
        textView.tag = i.toString()
        textView.setBackgroundColor(Color.BLACK)
        textView.setTextColor(Color.WHITE)
        textView.text = "Lorem ipsum " + i * 6847987465468486854
        textViewList.add(textView)
    }

    dragDropStaggeredGrid.setViews(textViewList, 5)
    //endregion
    
    //get a list of all the views
    val viewList = dragDropStaggeredGrid.getViews()

    //region update item with tag
    val textView = TextView(this)
    textView.setBackgroundColor(Color.BLACK)
    textView.setTextColor(Color.WHITE)
    textView.text = "Lorem ipsum"
    dragDropStaggeredGrid.setViewByTag(textView, "0")
    //endregion
    
    //get the view you want from the list with the tag you give it
    val view = dragDropStaggeredGrid.getViewByTag("0")
}

override fun dragging(localStateView: View, view: View?) {
    Log.i("i", "dragging")
}

override fun dragEnded(localStateView: View, view: View?) {
    Log.i("i", "drag ended")
    //Check if view changed places
    Log.i("Changed Places", dragDropStaggeredGrid.hasChangedPlaces().toString())
}

About

A staggered grid library with drag and drop capabilities for android.

Resources

License

Stars

Watchers

Forks

Packages

No packages published