신기성

clean and demo recyclerview

...@@ -36,6 +36,7 @@ dependencies { ...@@ -36,6 +36,7 @@ dependencies {
36 androidTestImplementation 'androidx.test:runner:1.2.0' 36 androidTestImplementation 'androidx.test:runner:1.2.0'
37 androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 37 androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
38 implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05' 38 implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05'
39 +
39 } 40 }
40 41
41 apply plugin: 'com.google.gms.google-services' 42 apply plugin: 'com.google.gms.google-services'
......
1 -package com.example.vip
2 -
3 -import android.view.LayoutInflater
4 -import android.view.ViewGroup
5 -import androidx.recyclerview.widget.RecyclerView
6 -import kotlinx.android.synthetic.main.field.view.*
7 -
8 -class MainAdapter : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
9 -
10 - var items: MutableList<PolicyField> = mutableListOf(PolicyField("Title1", "Content1"),
11 - PolicyField("Title2", "Content2"),PolicyField("Title3", "Content3"))
12 -
13 - override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = MainViewHolder(parent)
14 -
15 -
16 - override fun getItemCount(): Int = items.size
17 -
18 - override fun onBindViewHolder(holer: MainViewHolder, position: Int) {
19 - items[position].let { item ->
20 - with(holer) {
21 - tvTitle.text = item.title
22 - tvContent.text = item.content
23 - }
24 - }
25 - }
26 -
27 - inner class MainViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
28 - LayoutInflater.from(parent.context).inflate(R.layout.field, parent, false)) {
29 - val tvTitle = itemView.tv_main_title
30 - val tvContent = itemView.tv_main_content
31 - }
32 -}
1 -package com.example.vip
2 -
3 -class PolicyField(val title:String, val content:String)
...\ No newline at end of file ...\ No newline at end of file
1 +package com.example.vip
2 +
3 +import android.view.LayoutInflater
4 +import android.view.View
5 +import android.view.ViewGroup
6 +import android.widget.Toast
7 +import androidx.recyclerview.widget.RecyclerView
8 +import kotlinx.android.synthetic.main.list_item.view.*
9 +
10 +class RecyclerAdapter(private val items: ArrayList<YoutubeItem>) :
11 + RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
12 +
13 + override fun getItemCount() = items.size
14 +
15 + override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
16 + val item = items[position]
17 + val listener = View.OnClickListener {it ->
18 + Toast.makeText(it.context, "Clicked: ${item.title}", Toast.LENGTH_SHORT).show()
19 + }
20 + holder.apply {
21 + bind(listener, item)
22 + itemView.tag = item
23 + }
24 + }
25 +
26 + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
27 + RecyclerAdapter.ViewHolder {
28 + val inflatedView = LayoutInflater.from(parent.context)
29 + .inflate(R.layout.list_item, parent, false)
30 + return RecyclerAdapter.ViewHolder(inflatedView)
31 + }
32 +
33 + class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
34 +
35 + private var view: View = v
36 +
37 + fun bind(listener: View.OnClickListener, item: YoutubeItem) {
38 + view.thumbnail.setImageDrawable(item.image)
39 + view.title.text = item.title
40 + view.setOnClickListener(listener)
41 + }
42 + }
43 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -2,7 +2,7 @@ package com.example.vip ...@@ -2,7 +2,7 @@ package com.example.vip
2 2
3 import android.os.Bundle 3 import android.os.Bundle
4 import androidx.appcompat.app.AppCompatActivity 4 import androidx.appcompat.app.AppCompatActivity
5 -import androidx.recyclerview.widget.LinearLayoutManager 5 +import androidx.core.content.ContextCompat
6 import kotlinx.android.synthetic.main.activity_signin.* 6 import kotlinx.android.synthetic.main.activity_signin.*
7 7
8 class SignInActivity : AppCompatActivity() { 8 class SignInActivity : AppCompatActivity() {
...@@ -11,9 +11,23 @@ class SignInActivity : AppCompatActivity() { ...@@ -11,9 +11,23 @@ class SignInActivity : AppCompatActivity() {
11 super.onCreate(savedInstanceState) 11 super.onCreate(savedInstanceState)
12 setContentView(R.layout.activity_signin) 12 setContentView(R.layout.activity_signin)
13 13
14 - rv_field.adapter=MainAdapter() 14 + val list = ArrayList<YoutubeItem>()
15 - rv_field.layoutManager=LinearLayoutManager(this) 15 +
16 - } 16 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image01)!!, getString(R.string.title01)))
17 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image02)!!, getString(R.string.title02)))
18 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image03)!!, getString(R.string.title03)))
19 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image04)!!, getString(R.string.title04)))
20 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image05)!!, getString(R.string.title05)))
21 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image06)!!, getString(R.string.title06)))
22 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image07)!!, getString(R.string.title07)))
23 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image08)!!, getString(R.string.title08)))
24 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image09)!!, getString(R.string.title09)))
25 + list.add(YoutubeItem(ContextCompat.getDrawable(this,R.drawable.image10)!!, getString(R.string.title10)))
26 +
27 + val adapter = RecyclerAdapter(list)
28 + recyclerView.adapter = adapter
29 +
30 +}
17 31
18 32
19 } 33 }
......
1 +package com.example.vip
2 +
3 +import android.graphics.drawable.Drawable
4 +
5 +class YoutubeItem(val image: Drawable, val title: String) {
6 +
7 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -6,24 +6,23 @@ ...@@ -6,24 +6,23 @@
6 android:layout_height="match_parent" 6 android:layout_height="match_parent"
7 tools:context=".SignInActivity"> 7 tools:context=".SignInActivity">
8 8
9 + <LinearLayout
10 + android:layout_width="match_parent"
11 + android:layout_height="match_parent"
12 + android:orientation="vertical"
13 + tools:layout_editor_absoluteX="46dp"
14 + tools:layout_editor_absoluteY="287dp">
9 15
10 - <TextView 16 + <androidx.recyclerview.widget.RecyclerView
11 - android:id="@+id/textView" 17 + android:id="@+id/recyclerView"
12 - android:layout_width="wrap_content" 18 + android:layout_width="match_parent"
13 - android:layout_height="wrap_content" 19 + android:layout_height="match_parent"
14 - android:text="로그인완료" 20 + android:clipToPadding="false"
15 - app:layout_constraintBottom_toBottomOf="parent" 21 + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
16 - app:layout_constraintEnd_toEndOf="parent" 22 + tools:listitem="@layout/list_item"/>
17 - app:layout_constraintStart_toStartOf="parent" 23 +
18 - app:layout_constraintTop_toTopOf="parent" /> 24 +
25 + </LinearLayout>
19 26
20 - <androidx.recyclerview.widget.RecyclerView
21 - android:id="@+id/rv_field"
22 - android:layout_width="200dp"
23 - android:layout_height="50dp"
24 - app:layout_constraintBottom_toBottomOf="parent"
25 - app:layout_constraintEnd_toEndOf="parent"
26 - app:layout_constraintStart_toStartOf="parent"
27 - app:layout_constraintTop_toBottomOf="@+id/textView" />
28 27
29 </androidx.constraintlayout.widget.ConstraintLayout> 28 </androidx.constraintlayout.widget.ConstraintLayout>
...\ No newline at end of file ...\ No newline at end of file
......
1 -<?xml version="1.0" encoding="utf-8"?>
2 -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 - xmlns:tools="http://schemas.android.com/tools"
4 - android:orientation="vertical"
5 - android:layout_width="match_parent"
6 - android:layout_height="match_parent">
7 -
8 - <RelativeLayout
9 - android:layout_width="match_parent"
10 - android:layout_height="match_parent"
11 - android:layout_margin="20dp"
12 - android:orientation="vertical">
13 - <TextView
14 - android:id="@+id/tv_main_title"
15 - android:layout_width="wrap_content"
16 - android:layout_height="wrap_content"
17 - tools:text="TITLE"
18 - />
19 - <TextView
20 - android:id="@+id/tv_main_content"
21 - android:layout_width="match_parent"
22 - android:layout_height="wrap_content"
23 - android:layout_below="@id/tv_main_title"
24 - android:layout_marginTop="10dp"
25 - android:autoLink="web"
26 - tools:text="Content"/>
27 - </RelativeLayout>
28 -
29 -</LinearLayout>
...\ No newline at end of file ...\ No newline at end of file
1 +<?xml version="1.0" encoding="utf-8"?>
2 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 + xmlns:app="http://schemas.android.com/apk/res-auto"
4 + android:orientation="vertical"
5 + android:layout_width="match_parent"
6 + android:layout_height="match_parent">
7 +
8 +
9 + <ImageView
10 + android:id="@+id/thumbnail"
11 + android:layout_width="match_parent"
12 + android:layout_height="wrap_content"
13 + app:layout_constraintLeft_toLeftOf="parent"
14 + app:layout_constraintRight_toRightOf="parent"
15 + app:layout_constraintTop_toTopOf="parent"
16 + android:adjustViewBounds="true"/>
17 +
18 + <TextView
19 + android:id="@+id/title"
20 + android:layout_width="wrap_content"
21 + android:layout_height="wrap_content"
22 + android:textSize="20sp"
23 + app:layout_constraintTop_toBottomOf="@+id/thumbnail"
24 + app:layout_constraintLeft_toLeftOf="parent"
25 + app:layout_constraintRight_toRightOf="parent"/>
26 +
27 +</LinearLayout>
...\ No newline at end of file ...\ No newline at end of file
1 <resources> 1 <resources>
2 <string name="app_name">vip</string> 2 <string name="app_name">vip</string>
3 +
4 + <string name="title01">10 Best Practices for Moving to a Single Activity</string>
5 + <string name="title02">Cost of a Pixel Color (Android Dev Summit 18)</string>
6 + <string name="title03">Foldables, App Bundles and more from Android Dev Summit 18!</string>
7 + <string name="title04">Fun with LiveData (Android Dev Summit 18)</string>
8 + <string name="title05">Keynote (Android Dev Summit 18)</string>
9 + <string name="title06">Modern WebView Best Practices (Android Dev Summit 18)</string>
10 + <string name="title07">Performance Analysis Using Systrace (Android Dev Summit 18)</string>
11 + <string name="title08">Preferential Practices for Preferences (Android Dev Summit 18)</string>
12 + <string name="title09">That’s a wrap on Android Dev Summit 2018!</string>
13 + <string name="title10">Vitals: Past, Present and Future (Android Dev Summit 18)</string>
3 </resources> 14 </resources>
......