Skip to content

Commit

Permalink
(#2) Add Expandable textview feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
myung6024 authored and myung jun Hyun committed Jan 22, 2020
1 parent cdf6961 commit 9c3557e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 25 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "androidx.appcompat:appcompat:$appcompat_version"
implementation 'androidx.core:core-ktx:1.1.0'
implementation "androidx.fragment:fragment:1.2.0-rc02"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Expand All @@ -61,7 +61,6 @@ dependencies {
}

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "androidx.appcompat:appcompat:$appcompat_version"
implementation "androidx.constraintlayout:constraintlayout:$constraint_version"
implementation "androidx.cardview:cardview:1.0.0"

Expand Down
33 changes: 31 additions & 2 deletions app/src/main/java/com/mashup/app/notices/NoticeListBindings.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.mashup.app.notices

import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.mashup.R
import com.mashup.model.Notice
import com.mashup.model.NoticeAttendance
import com.mashup.model.VoteStatus
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import java.util.*
Expand Down Expand Up @@ -40,3 +39,33 @@ fun setVoteTextColor(textView: TextView, isSelected: Boolean) {
else
textView.setTextColor(ContextCompat.getColor(textView.context, R.color.colorPrimary))
}

@BindingAdapter(value = ["app:seeMoreVisibility", "app:position", "app:setPosition"])
fun setSeeMoreTextVisibility(textView: TextView, targetText: TextView, position: Int, setPosition : (Int) -> Unit) {
targetText.post {
if (targetText.lineCount >= 5) {
textView.setOnClickListener {
if (targetText.maxLines == 100) {
targetText.maxLines = 5
textView.text = textView.context.getString(R.string.notice_see_more)
} else {
targetText.maxLines = 100
textView.text = textView.context.getString(R.string.notice_collapse)
}
setPosition(position)
}
targetText.setOnClickListener {
if (targetText.maxLines == 100) {
targetText.maxLines = 5
textView.text = textView.context.getString(R.string.notice_see_more)
} else {
targetText.maxLines = 100
textView.text = textView.context.getString(R.string.notice_collapse)
}
setPosition(position)
}
textView.visibility = View.VISIBLE
} else
textView.visibility = View.GONE
}
}
20 changes: 16 additions & 4 deletions app/src/main/java/com/mashup/app/notices/NoticesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,44 @@ package com.mashup.app.notices

import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.animation.OvershootInterpolator
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.mashup.R
import com.mashup.databinding.NoticeItemBinding
import com.mashup.model.Notice

class NoticeAdapter(private val viewModel: NoticesViewModel) :
ListAdapter<Notice, NoticeAdapter.ViewHolder>(TaskDiffCallback()) {
ListAdapter<Notice, NoticeAdapter.ViewHolder>(TaskDiffCallback()) {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)

holder.bind(viewModel, item)
holder.bind(viewModel, item, position)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}

class ViewHolder private constructor(val binding: NoticeItemBinding) :
RecyclerView.ViewHolder(binding.root) {
RecyclerView.ViewHolder(binding.root) {

fun bind(viewModel: NoticesViewModel, item: Notice) {
fun bind(viewModel: NoticesViewModel, item: Notice, position: Int) {

if (position == viewModel.expandPosition) {
binding.descriptionText.maxLines = 100
binding.toggleButton.text =
binding.toggleButton.context.getString(R.string.notice_collapse)
} else {
binding.descriptionText.maxLines = 5
binding.toggleButton.text =
binding.toggleButton.context.getString(R.string.notice_see_more)
}
binding.viewmodel = viewModel
binding.notice = item
binding.position = position
binding.executePendingBindings()
}

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/mashup/app/notices/NoticesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class NoticesViewModel(

private lateinit var authToken: AuthToken

var expandPosition: Int = -1

init {
checkAuthToken()
getNotice(false)
Expand Down Expand Up @@ -121,6 +123,15 @@ class NoticesViewModel(
}
}

val setExpandPosition: (Int) -> Unit
get() = fun(position: Int) {
expandPosition = if (expandPosition == position) {
-1
} else {
position
}
}

override fun onCleared() {
compositeDisposable.clear()
super.onCleared()
Expand Down
48 changes: 32 additions & 16 deletions app/src/main/res/layout/notice_item.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<layout 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">

<data>

<import type="android.view.View"/>
<import type="android.view.View" />

<import type="android.widget.CompoundButton" />

Expand All @@ -20,17 +20,20 @@
<variable
name="viewmodel"
type="com.mashup.app.notices.NoticesViewModel" />

<variable
name="position"
type="Integer" />
</data>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="50dp"
android:onClick="@{() -> viewmodel.onClickItem(notice)}"
app:cardElevation="@dimen/card_elevation"
app:cardCornerRadius="@dimen/card_radius">
app:cardCornerRadius="@dimen/card_radius"
app:cardElevation="@dimen/card_elevation">

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -88,8 +91,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="36dp"
android:drawableStart="@drawable/icon_date"
android:drawablePadding="4dp"
android:gravity="center_vertical"
Expand All @@ -103,8 +106,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="36dp"
android:drawableStart="@drawable/icon_clock"
android:drawablePadding="4dp"
android:gravity="center_vertical"
Expand All @@ -118,8 +121,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="36dp"
android:drawableStart="@drawable/icon_place"
android:drawablePadding="4dp"
android:gravity="center_vertical"
Expand All @@ -135,19 +138,32 @@
android:background="@color/line" />

<TextView
android:id="@+id/descriptionText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="24dp"
android:layout_weight="1"
android:text="@{notice.description}"
android:ellipsize="end"
android:maxLines="5"
android:text="@{notice.description}"
android:textColor="@color/text"
android:textSize="@dimen/text_small"
tools:text="MASH-UP" />

<TextView
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/notice_see_more"
android:textSize="@dimen/text_small"
android:visibility="gone"
app:position="@{position}"
app:seeMoreVisibility="@{descriptionText}"
app:setPosition="@{viewmodel.setExpandPosition}" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
Expand Down Expand Up @@ -205,8 +221,8 @@
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="@{@string/notice_tv_vote_attendance(notice.voteAttendanceCount)}"
app:voteTextColor="@{notice.userAttendance == VoteStatus.ATTEND}"
android:textSize="@dimen/text_small" />
android:textSize="@dimen/text_small"
app:voteTextColor="@{notice.userAttendance == VoteStatus.ATTEND}" />
</FrameLayout>

<View
Expand All @@ -231,9 +247,9 @@
android:gravity="center"
android:paddingTop="16dp"
android:paddingBottom="16dp"
app:voteTextColor="@{notice.userAttendance == VoteStatus.ABSENT}"
android:text="@{@string/notice_tv_vote_absent(notice.voteAbsentCount)}"
android:textSize="@dimen/text_small" />
android:textSize="@dimen/text_small"
app:voteTextColor="@{notice.userAttendance == VoteStatus.ABSENT}" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
<string name="setting_tv_logout">로그아웃하기</string>
<string name="title_notice">전체공지</string>
<string name="title_setting">설정</string>

<string name="notice_see_more">더 보기</string>
<string name="notice_collapse">다시 접기</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
kotlin_version = '1.3.11'
appcompat_version="1.0.2"
appcompat_version="1.1.0-rc01"
lifecycle_version = "2.0.0"
constraint_version = "2.0.0-beta2"

Expand Down

0 comments on commit 9c3557e

Please sign in to comment.