Skip to content

Commit

Permalink
Better UI, add animations & dividers
Browse files Browse the repository at this point in the history
  • Loading branch information
Senorsen committed Aug 29, 2017
1 parent 34b83f1 commit cc95485
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions wukong/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.senorsen.wukong"
minSdkVersion 21
targetSdkVersion 25
versionCode 147
versionName "0.1.47"
versionCode 148
versionName "0.1.48"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.senorsen.wukong.ui

import android.content.Context
import android.graphics.Canvas
import android.support.v7.widget.RecyclerView
import android.graphics.drawable.Drawable
import com.senorsen.wukong.R


class DividerItemDecoration(context: Context, private val leftInset: Int = 0) : RecyclerView.ItemDecoration() {

private val mDivider: Drawable

init {
mDivider = context.getResources().getDrawable(R.drawable.line_divider)
}

override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val left = parent.paddingLeft + leftInset
val right = parent.width - parent.paddingRight

val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)

val params = child.layoutParams as RecyclerView.LayoutParams

val top = child.bottom + params.bottomMargin
val bottom = top + mDivider.intrinsicHeight

mDivider.setBounds(left, top, right, bottom)
mDivider.draw(c)
}
}
}
15 changes: 12 additions & 3 deletions wukong/src/main/java/com/senorsen/wukong/ui/SongListFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class SongListFragment : Fragment() {
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.setHasFixedSize(false)
recyclerView.itemAnimator = SlideInRightAnimator()
recyclerView.addItemDecoration(DividerItemDecoration(activity))

songListLocalStore = SongListLocalStore(this.activity)

Expand Down Expand Up @@ -214,6 +215,7 @@ class SongListFragment : Fragment() {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val song = filteredList?.getOrNull(position) ?: return
holder.icon.text = holder.layoutPosition.toString()
holder.name.text = song.title
holder.caption.text = "${song.artist} - ${song.album}"
holder.songKey = song.songKey
Expand Down Expand Up @@ -257,7 +259,7 @@ class SongListFragment : Fragment() {

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

val icon = view.findViewById(android.R.id.icon) as ImageView
val icon = view.findViewById(android.R.id.icon) as TextView
val name = view.findViewById(android.R.id.text1) as TextView
val caption = view.findViewById(android.R.id.text2) as TextView
val upIcon = view.findViewById(R.id.song_list_up) as ImageView
Expand All @@ -271,7 +273,9 @@ class SongListFragment : Fragment() {
val tempList = list!!.toMutableList()
val song = filteredList!!.find { it.songKey == songKey }!!

notifyItemMoved(filteredList!!.indexOf(song), 0)
val index = adapterPosition
notifyItemMoved(index, 0)
notifyItemRangeChanged(0, index + 1)
recyclerView.layoutManager.scrollToPosition(0)

tempList.remove(song)
Expand All @@ -291,7 +295,12 @@ class SongListFragment : Fragment() {
val tempList = list!!.toMutableList()
val song = filteredList!!.find { it.songKey == songKey }

notifyItemRemoved(filteredList!!.indexOf(song))
val index = adapterPosition
notifyItemRemoved(index)
// If there are more elements after this, should update their index number.
if (filteredList!!.size > index) {
notifyItemRangeChanged(index, filteredList!!.size - index)
}

tempList.remove(song)
list = tempList
Expand Down
10 changes: 10 additions & 0 deletions wukong/src/main/res/drawable/line_divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="0.5px" />

<solid android:color="#ddd" />

</shape>
7 changes: 4 additions & 3 deletions wukong/src/main/res/layout/item_song.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dp"
android:minHeight="70dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingStart="16dp">

<ImageView
<TextView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginEnd="16dp"
android:layout_weight="0"
tools:src="@drawable/netease" />
tools:text="1" />

<LinearLayout
android:layout_width="0dp"
Expand Down

0 comments on commit cc95485

Please sign in to comment.