Skip to content

Commit

Permalink
Added an example using Motion Scene Swipe Up
Browse files Browse the repository at this point in the history
  • Loading branch information
riggaroo committed Dec 19, 2018
1 parent 6464ca4 commit dfd7249
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 7 deletions.
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.google.android.material:material:1.0.0-rc02'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.android.material:material:1.0.0'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<activity android:name=".CircularConstraintActivity" />
<activity android:name=".ImageFilterViewActivity"/>
<activity android:name=".ConstraintLayoutStatesExampleActivity"/>
<activity android:name=".BottomNavAnimationActivity"/>
<activity android:name=".RecyclerViewSwipeMotionActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class OptionsActivity : AppCompatActivity() {
buttonClStates.setOnClickListener {
startActivity(Intent(this, ConstraintLayoutStatesExampleActivity::class.java))
}

buttonSwipeRecyclerView.setOnClickListener {
startActivity(Intent(this, RecyclerViewSwipeMotionActivity::class.java))
}
}
}

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 removed app/src/main/res/drawable/dvt_hyde_park.png
Binary file not shown.
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@
app:layout_constraintTop_toBottomOf="@+id/buttonImageFilterView"
android:text="STATES"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSwipeRecyclerView"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/buttonClStates"
android:text="MOTION SWIPE RECYCLERVIEW"/>

</androidx.constraintlayout.widget.ConstraintLayout>
38 changes: 38 additions & 0 deletions app/src/main/res/layout/activity_swipe_recycler_view.xml
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>
47 changes: 47 additions & 0 deletions app/src/main/res/layout/list_item_email.xml
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>
2 changes: 1 addition & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<resources>
<color name="colorPrimary">#26a69a</color>
<color name="colorPrimaryDark">#00bfa5</color>
<color name="colorAccent">#FF1744</color>
<color name="colorAccent">#B39DDB</color>
<color name="gray_divider">#eeeeee</color>
<color name="white">#ffffff</color>
<color name="light_gray">#eeeeee</color>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/res/xml/motion_scene_swipe.xml
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>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.51'
ext.kotlin_version = '1.3.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-beta04'
classpath 'com.android.tools.build:gradle:3.3.0-rc02'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit dfd7249

Please sign in to comment.