-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example using Motion Scene Swipe Up
- Loading branch information
Showing
12 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/za/co/riggaroo/constraintlayoutdemo/RecyclerViewSwipeMotionActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package za.co.riggaroo.constraintlayoutdemo | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.util.TypedValue | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.android.material.bottomappbar.BottomAppBarTopEdgeTreatment | ||
import com.google.android.material.shape.CornerTreatment | ||
import com.google.android.material.shape.MaterialShapeDrawable | ||
import com.google.android.material.shape.RoundedCornerTreatment | ||
import com.google.android.material.shape.ShapePathModel | ||
import kotlinx.android.synthetic.main.activity_swipe_recycler_view.* | ||
import kotlinx.android.synthetic.main.list_item_email.view.* | ||
|
||
class RecyclerViewSwipeMotionActivity : AppCompatActivity(){ | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_swipe_recycler_view) | ||
|
||
val shapePathModel = ShapePathModel().apply { | ||
val margin = applicationContext.dipToPixels(36f) | ||
val corner = RoundedCornerTreatment(margin) | ||
val noCorner = CornerTreatment() | ||
setCornerTreatments(corner, corner, noCorner, noCorner) | ||
topEdge = BottomAppBarTopEdgeTreatment(margin, margin, margin) | ||
} | ||
|
||
val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply { | ||
setTint(ContextCompat.getColor(this@RecyclerViewSwipeMotionActivity, R.color.white)) | ||
isShadowEnabled = true | ||
} | ||
background_view.background = backgroundDrawable | ||
|
||
val listItems = listOf(Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!"), | ||
Email("Rebecca Franks", "It's the holidays!")) | ||
|
||
val emailAdapter = EmailAdapter() | ||
|
||
recyclerViewEmails.adapter = emailAdapter | ||
recyclerViewEmails.layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false) | ||
|
||
emailAdapter.submitList(listItems) | ||
} | ||
|
||
fun Context.dipToPixels(dipValue: Float) = | ||
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, resources.displayMetrics) | ||
|
||
data class Email(val name: String, val subject: String) | ||
|
||
|
||
class EmailAdapter: ListAdapter<Email, EmailViewHolder>(DIFFER){ | ||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmailViewHolder { | ||
return EmailViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item_email, parent, false)) | ||
} | ||
|
||
override fun onBindViewHolder(holder: EmailViewHolder, position: Int) { | ||
holder.bind(getItem(position)) | ||
} | ||
|
||
companion object { | ||
val DIFFER = object : DiffUtil.ItemCallback<Email>() { | ||
override fun areItemsTheSame(oldItem: Email, newItem: Email): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: Email, newItem: Email): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} | ||
} | ||
|
||
class EmailViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
|
||
fun bind(email : Email){ | ||
itemView.textViewEmailName.text = email.name | ||
itemView.textViewSubject.text = email.subject | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.motion.widget.MotionLayout | ||
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" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:background="?attr/colorAccent" | ||
app:layoutDescription="@xml/motion_scene_swipe" | ||
android:layout_height="match_parent"> | ||
|
||
<View | ||
android:id="@+id/background_view" | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
app:layout_constraintTop_toTopOf="parent" | ||
android:layout_marginTop="72dp" | ||
android:background="?attr/colorBackgroundFloating" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent"> | ||
|
||
</View> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
app:layout_constraintTop_toTopOf="@+id/background_view" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
tools:listitem="@layout/list_item_email" | ||
android:clipToPadding="false" | ||
android:id="@+id/recyclerViewEmails"> | ||
|
||
</androidx.recyclerview.widget.RecyclerView> | ||
|
||
</androidx.constraintlayout.motion.widget.MotionLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout 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" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<TextView | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/textViewEmailName" | ||
app:layout_constraintStart_toEndOf="@+id/imageViewAvatar" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginTop="16dp" | ||
app:layout_constraintTop_toTopOf="parent" | ||
android:textAppearance="@style/TextAppearance.AppCompat.Medium" | ||
android:layout_marginEnd="16dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
tools:text="@tools:sample/full_names"/> | ||
|
||
<ImageView | ||
android:layout_width="64dp" | ||
android:layout_height="64dp" | ||
tools:srcCompat="@tools:sample/avatars" | ||
android:id="@+id/imageViewAvatar" | ||
app:layout_constraintStart_toStartOf="parent" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginTop="16dp" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
|
||
<TextView | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/textViewSubject" | ||
app:layout_constraintStart_toEndOf="@+id/imageViewAvatar" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginTop="16dp" | ||
app:layout_constraintTop_toBottomOf="@+id/textViewEmailName" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
android:layout_marginEnd="16dp" | ||
tools:text="@tools:sample/lorem/random" | ||
android:lines="2" | ||
android:ellipsize="end" | ||
android:layout_marginBottom="8dp" | ||
app:layout_constraintBottom_toBottomOf="parent"/> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:motion="http://schemas.android.com/apk/res-auto"> | ||
|
||
<Transition | ||
motion:constraintSetStart="@id/start" | ||
motion:constraintSetEnd="@id/end" | ||
motion:duration="1000"> | ||
<OnSwipe | ||
motion:touchAnchorId="@+id/recyclerViewEmails" | ||
motion:touchAnchorSide="top" | ||
motion:dragDirection="dragUp" /> | ||
</Transition> | ||
|
||
<ConstraintSet android:id="@+id/start"> | ||
<Constraint | ||
android:id="@id/background_view" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_marginTop="72dp" | ||
motion:layout_constraintBottom_toBottomOf="parent" | ||
motion:layout_constraintStart_toStartOf="parent" | ||
motion:layout_constraintEnd_toEndOf="parent" | ||
motion:layout_constraintTop_toTopOf="parent"> | ||
</Constraint> | ||
</ConstraintSet> | ||
|
||
<ConstraintSet android:id="@+id/end"> | ||
<Constraint | ||
android:id="@id/background_view" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_marginTop="0dp" | ||
motion:layout_constraintBottom_toBottomOf="parent" | ||
motion:layout_constraintStart_toStartOf="parent" | ||
motion:layout_constraintEnd_toEndOf="parent" | ||
motion:layout_constraintTop_toTopOf="parent"> | ||
</Constraint> | ||
</ConstraintSet> | ||
</MotionScene> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters