신기성

clean and demo recyclerview

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