Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/12 reccomend view #18

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

buildFeatures{
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
android:theme="@style/Theme.NotToDo"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".RecommendActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>

<service
android:name=".FirebaseMessagingService"
Expand All @@ -24,6 +31,7 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<activity
android:name=".presentation.MainActivity"
android:exported="true">
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/kr/co/nottodo/RecommendActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.co.nottodo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class RecommendActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_recommendation)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kr.co.nottodo.R
import kr.co.nottodo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package kr.co.nottodo.presentation.toplevel.recommendation

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import kr.co.nottodo.R
import kr.co.nottodo.databinding.ItemRecommendBinding
import kr.co.nottodo.presentation.toplevel.recommendation.data.Recommendationdata

class RecommendationAdapter(private val tools: List<Recommendationdata>) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapter를 생성할 때는 list는 안받습니다!

이렇게 구현이 된다면 순서를 생각해봤을 때

  1. 서버에서 리스트 데이터를 받는다.

  2. 받은 리스트 데이터를 활용해 어댑터를 만든다.

이 순서가 되는데 그게 아니라

  1. 어댑터를 만들어 놓는다.

  2. 서버에서 데이터가 들어오면 어댑터에 비어 있는 리스트에 담아둔다!

는 순서로 바꿔야 됩니다!

RecyclerView.Adapter<RecommendationViewHolder>() {

private val sampleItems = mutableListOf<Recommendationdata>()
private val items: List<Recommendationdata> = tools
private var selectedPosition = -1
private var lastItemSelectedPosition = -1


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
RecommendationViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding: ItemRecommendBinding =
DataBindingUtil.inflate(layoutInflater, R.layout.item_recommend, parent, false)
return RecommendationViewHolder(binding)
}

override fun onBindViewHolder(
holder: RecommendationViewHolder, position: Int
) {
if (position == selectedPosition) {
selectBackground(holder, sampleItems[position])
} else {
normalBackground(holder, sampleItems[position])
}
bind(holder, position)
}

override fun getItemCount(): Int = sampleItems.size

fun submitList(list: List<Recommendationdata>) {
sampleItems.clear()
sampleItems.addAll(list)
notifyDataSetChanged()
}

private fun bind(
holder: RecommendationViewHolder,
position: Int
) {
if (!holder.itemView.hasOnClickListeners()) {
holder.itemView.setOnClickListener {
selectedPosition = position
lastItemSelectedPosition = if (lastItemSelectedPosition == -1) {
selectedPosition
} else {
notifyItemChanged(lastItemSelectedPosition)
selectedPosition
}
notifyItemChanged(selectedPosition)
}
}
}

private fun normalBackground(
holder: RecommendationViewHolder,
data: Recommendationdata
) {
holder.binding.apply {
this.root.setBackgroundColor(Color.parseColor("#334455"))
executePendingBindings()
}
}

private fun selectBackground(
holder: RecommendationViewHolder,
data: Recommendationdata
) {
holder.binding.apply {
this.root.setBackgroundColor(Color.parseColor("#112233"))
executePendingBindings()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package kr.co.nottodo.presentation.toplevel.recommendation

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.NonDisposableHandle.parent
import kr.co.nottodo.R
import kr.co.nottodo.databinding.FragmentRecommendationBinding
import kr.co.nottodo.databinding.ItemRecommendBinding
import kr.co.nottodo.presentation.toplevel.recommendation.data.Recommendationdata

class RecommendViewHolder(val binding: ItemRecommendBinding) : RecyclerView.ViewHolder(binding.root)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요건 따로 파일하나 만들어서 관리하는건 어떨까요?!


class RecommendationFragment : Fragment() {

private lateinit var binding: FragmentRecommendationBinding
private lateinit var recommendationAdapter: RecommendationAdapter

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = DataBindingUtil.inflate<FragmentRecommendationBinding>(
inflater,R.layout.fragment_recommendation, container,false
)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 RecyclerView의 제약조건이 match 로 걸려서 꽉 차보이는 거 일 거같아요.

width = match_parent, height = wrap_content로 변경해보면 어떨까요?

// 여기서 프라그먼트 작업을 해주시면 좋습니다
val itemList: List<Recommendationdata> = listOf(Recommendationdata("ㅎㅇ", "ㅎㅇ"), Recommendationdata("ㅎㅇ", "ㅎㅇ") )
recommendationAdapter = RecommendationAdapter(itemList)

binding.rvRecommendation.adapter = recommendationAdapter
binding.rvRecommendation.apply {
layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.co.nottodo.presentation.toplevel.recommendation

import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import kr.co.nottodo.databinding.ItemRecommendBinding
import kr.co.nottodo.presentation.toplevel.recommendation.data.Recommendationdata

class RecommendationViewHolder(
val binding: ItemRecommendBinding
) : RecyclerView.ViewHolder(binding.root) {

fun onBind(data: Recommendationdata) {
Glide.with(binding.root.context)
.load("https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory&fname=https://k.kakaocdn.net/dn/EShJF/btquPLT192D/SRxSvXqcWjHRTju3kHcOQK/img.png")
.into(binding.ivRecommend)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.co.nottodo.presentation.toplevel.recommendation.data

data class Recommendationdata(
val image: String, // 서버에서 이미지 url이 내려오는 경우 String으로 받아야합니다.
val name: String,
)
57 changes: 57 additions & 0 deletions app/src/main/res/layout/fragment_recommendation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<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>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecommendActivity">

<EditText
android:id="@+id/et_recommend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="94dp"
android:layout_marginTop="37dp"
android:layout_marginBottom="21dp"
android:text="추천받기"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="@id/divider"
app:layout_constraintStart_toEndOf="@id/btn_recommendation_back"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="21dp"
android:backgroundTint="@color/gray_1_626068"
app:layout_constraintTop_toBottomOf="@id/et_recommend" />

<ImageButton
android:id="@+id/btn_recommendation_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="38dp"
android:src="@drawable/ic_btn_back"
app:layout_constraintEnd_toStartOf="@id/et_recommend"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recommendation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/divider"
app:spanCount="5"
tools:listitem="@layout/item_recommend" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
39 changes: 39 additions & 0 deletions app/src/main/res/layout/item_recommend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<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">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv_recommend"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_sns_active" />

<TextView
android:id="@+id/tv_recommend_title"
style="@style/B12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginBottom="12dp"
android:textColor="@color/gray_1_626068"
android:text="민영 테스트"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/iv_recommend"
app:layout_constraintStart_toStartOf="@id/iv_recommend"
app:layout_constraintTop_toBottomOf="@id/iv_recommend"
tools:text="민영 테스트" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<resources>
<string name="app_name">NotToDo</string>

<!--home-->
<string name="home_menu_home">홈</string>
<string name="home_menu_recommend">추천</string>
Expand Down