Skip to content

Commit

Permalink
Feat: Card 에 Tag 란 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
linenive committed Jun 20, 2021
1 parent 8f8b04a commit 13100d2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
105 changes: 104 additions & 1 deletion app/src/main/java/kr/ac/konkuk/koogle/Adapter/CardAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package kr.ac.konkuk.koogle.Adapter

import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.core.view.children
import androidx.core.view.setMargins
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kr.ac.konkuk.koogle.DBKeys
import kr.ac.konkuk.koogle.Model.ArticleModel
import kr.ac.konkuk.koogle.Model.CardModel
import kr.ac.konkuk.koogle.Model.TagModel
import kr.ac.konkuk.koogle.R
import kr.ac.konkuk.koogle.databinding.ItemCardBinding

class CardAdapter: ListAdapter<CardModel, CardAdapter.ViewHolder>(diffUtil){
// 2021-06-20 주예진 수정: context 추가(tag TextView를 동적으로 추가시켜주기 위해 사용)
class CardAdapter(val context: Context?): ListAdapter<CardModel, CardAdapter.ViewHolder>(diffUtil){

//리스너 정의
interface OnItemClickListener {
Expand All @@ -27,6 +41,7 @@ class CardAdapter: ListAdapter<CardModel, CardAdapter.ViewHolder>(diffUtil){
var itemClickListener: OnItemClickListener? = null

inner class ViewHolder(private val binding: ItemCardBinding): RecyclerView.ViewHolder(binding.root) {
var tagView: LinearLayout = itemView.findViewById(R.id.tagView)
fun bind(cardModel: CardModel){
binding.nicknameTextView.text = cardModel.writerName
binding.titleTextView.text = cardModel.articleTitle
Expand All @@ -35,6 +50,20 @@ class CardAdapter: ListAdapter<CardModel, CardAdapter.ViewHolder>(diffUtil){
binding.currentNumberTextView.text = cardModel.currentNumber.toString()
binding.locationTextView.text = cardModel.desiredLocation?.fullAddress ?: ""

// Tag List
if(cardModel.tagList!=null){
var newTagList = arrayListOf<String>()

// 데이타 파싱
for(t in cardModel.tagList!!.children){
for(st in t.child(DBKeys.SUB_TAGS).children){
newTagList.add(st.key.toString())
}
}
// 뷰에 적용
settingTagDataToView(newTagList)
}

//glide 사용
if(cardModel.writerProfileImageUrl.isNotEmpty()){
Glide.with(binding.profileImageView)
Expand Down Expand Up @@ -74,6 +103,80 @@ class CardAdapter: ListAdapter<CardModel, CardAdapter.ViewHolder>(diffUtil){
)
}
}
private fun getRow(index: Int): LinearLayout{
return (tagView.getChildAt(index) as ScrollView).getChildAt(0) as LinearLayout
}
// 태그 row 추가
private fun addRow() {
val row = LinearLayout(context)
val lp: LinearLayout.LayoutParams =
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
row.orientation = LinearLayout.HORIZONTAL
row.layoutParams = lp

val newScrollView = ScrollView(context)

val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
newScrollView.layoutParams = layoutParams

val linearParams = LinearLayout.LayoutParams(
800,
ViewGroup.LayoutParams.WRAP_CONTENT
)
row.orientation = LinearLayout.HORIZONTAL
row.layoutParams = linearParams

newScrollView.addView(row)
tagView.addView(newScrollView)
}
// 태그 리스트를 여러 줄로 분할해 동적으로 생성
private fun settingTagDataToView(data: ArrayList<String>){
var lastRow: LinearLayout
for (tag in data) {
// row 가 하나도 없으면 새로 만들기
if (tagView.childCount == 0) {
addRow()
}
// 새로운 Table row 를 추가해야 하는지 길이 검사
lastRow = getRow(tagView.childCount - 1)
var len = 0
val row_len = 26
val margin = 1
for (i in lastRow.children) {
i as TextView
len += i.text.length + margin
}
len += tag.length
if (len > row_len) {
addRow()
}
lastRow = getRow(tagView.childCount - 1)

lastRow.addView(makeSubTagView(tag))
}
}
// SubTag 한 칸을 생성한다.
fun makeSubTagView(tagName: String): TextView {
var subTagText = TextView(context)
subTagText.setText(tagName)
// 모서리가 둥근 태그 스타일 적용(임시)
subTagText.setTextAppearance(R.style.TAG_STYLE)
subTagText.setBackgroundResource(R.drawable.layout_tag_background)
// 태그 간 간격 설정
val p = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
p.setMargins(5)
subTagText.layoutParams = p
return subTagText
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/kr/ac/konkuk/koogle/Adapter/TagAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class TagAdapter(val context: Context, val data: MutableList<TagModel>,
// 모서리가 둥근 태그 스타일 적용(임시)
subTagText.setTextAppearance(R.style.TAG_STYLE)
subTagText.setBackgroundResource(R.drawable.layout_tag_background)
subTagText.setBackgroundResource(R.drawable.layout_tag_background)
subTagText.maxLines = 1
subTagText.ellipsize = TextUtils.TruncateAt.MARQUEE
subTagText.isSingleLine = true
Expand All @@ -207,7 +206,7 @@ class TagAdapter(val context: Context, val data: MutableList<TagModel>,
if (subTagView.childCount == 0) {
addRow()
}
// 새로운 Table row 를 추가해야 하는지 길이 검사(임시)
// 새로운 Table row 를 추가해야 하는지 길이 검사
lastRow = getRow(subTagView.childCount - 1)
var len = 0
val row_len = 26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kr.ac.konkuk.koogle.Activity.ArticleActivity
import kr.ac.konkuk.koogle.Adapter.CardAdapter
import kr.ac.konkuk.koogle.DBKeys.Companion.ARTICLE_ID
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_ARTICLES
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_MAIN_TAGS
import kr.ac.konkuk.koogle.DBKeys.Companion.DB_USERS
import kr.ac.konkuk.koogle.DBKeys.Companion.WRITER_ID
import kr.ac.konkuk.koogle.Model.ArticleModel
Expand Down Expand Up @@ -48,6 +49,7 @@ class CardFragment : Fragment(), CardStackListener {
{
val cardModel = snapshot.getValue(CardModel::class.java)
if (cardModel != null) {
cardModel.tagList = snapshot.child(DB_MAIN_TAGS)
cardList.add(cardModel)
}
cardAdapter.submitList(cardList)
Expand Down Expand Up @@ -80,7 +82,7 @@ class CardFragment : Fragment(), CardStackListener {
}

private fun initCardStackView() {
cardAdapter = CardAdapter()
cardAdapter = CardAdapter(context)

binding?.cardStackView?.layoutManager = CardStackLayoutManager(context, this)
binding?.cardStackView?.adapter = cardAdapter
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/kr/ac/konkuk/koogle/Model/CardModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.ac.konkuk.koogle.Model

import com.google.firebase.database.DataSnapshot
import kr.ac.konkuk.koogle.Model.Entity.SearchResultEntity

data class CardModel(
Expand All @@ -13,10 +14,14 @@ data class CardModel(
val articleContent:String,//글 내용
val currentNumber:Int,// 현재 모집된 인원
val recruitmentNumber:Int, //모집 인원
val desiredLocation: SearchResultEntity? //만남 희망 장소
val desiredLocation: SearchResultEntity?, //만남 희망 장소
var tagList: DataSnapshot? // 태그 리스트
){
// constructor(): this("","","", "", "", "","",0,0, null)
//임시 수정
constructor(): this("","","","", "", "","",0,0, null)
constructor(): this("","","","", "",
"","",0,0, null,
null
)
}

7 changes: 7 additions & 0 deletions app/src/main/res/layout/item_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
android:padding="20dp"
android:hint="@string/content" />

<LinearLayout
android:id="@+id/tagView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="vertical"></LinearLayout>

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

0 comments on commit 13100d2

Please sign in to comment.