Skip to content

Commit 4d3d627

Browse files
committed
study:
- SwipeRefreshLayout 연습 3 - Image, text, 새로고침 완료
1 parent b3a411f commit 4d3d627

5 files changed

Lines changed: 78 additions & 34 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.seok.gfd.adapter
2+
3+
import android.content.Context
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.ImageView
8+
import android.widget.TextView
9+
import android.widget.Toast
10+
import androidx.recyclerview.widget.RecyclerView
11+
import com.seok.gfd.R
12+
13+
class CustomAdapter(personNames : ArrayList<String>, personImages : ArrayList<Int>, context: Context) : RecyclerView.Adapter<CustomAdapter.MyViewHolder>() {
14+
var personNames =personNames
15+
var personImages : ArrayList<Int> = personImages
16+
var context: Context = context
17+
18+
override fun getItemCount() = personNames.size
19+
20+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
21+
val v = LayoutInflater.from(parent.context).inflate(R.layout.rowlayout, parent, false)
22+
return MyViewHolder(v)
23+
}
24+
25+
26+
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
27+
holder.name.text = personNames[position]
28+
holder.image.setImageResource(personImages[position])
29+
holder.itemView.setOnClickListener {
30+
Toast.makeText(context, personNames[position], Toast.LENGTH_SHORT).show()
31+
}
32+
}
33+
34+
inner class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
35+
var name : TextView = itemView.findViewById(R.id.name)
36+
var image : ImageView = itemView.findViewById(R.id.image)
37+
}
38+
39+
}
40+
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.seok.gfd.views
22

33
import android.os.Bundle
4-
import android.widget.ArrayAdapter
54
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.recyclerview.widget.LinearLayoutManager
66
import com.seok.gfd.R
7+
import com.seok.gfd.adapter.CustomAdapter
78
import kotlinx.android.synthetic.main.activity_guest_main.*
89
import java.util.*
910

1011
class GuestMain : AppCompatActivity() {
11-
private val arrayList =ArrayList<String>(listOf("First Element", "Second Element", "Third Element", "Fourth Element", "Fifth Element"))
12+
private val personNames =ArrayList(listOf("Person 1", "Person 2", "Person 3", "Person 4", "Person 5", "Person 6", "Person 7", "Person 8", "Person 9", "Person 10", "Person 11", "Person 12", "Person 13", "Person 14"))
13+
private val personImages = ArrayList(listOf(R.drawable.btn_bottom_help, R.drawable.icon_location, R.drawable.btn_bottom_home, R.drawable.btn_bottom_rank, R.drawable.gfd_app_logo, R.drawable.gfd_launcher, R.drawable.gfd_logo_background, R.drawable.gfd_logo_foreground, R.drawable.github_login_users_background, R.drawable.guest_login_background, R.drawable.ic_launcher_background, R.drawable.icon_bio, R.drawable.rounding_trans_background, R.drawable.rounding_background))
1214

1315
override fun onCreate(savedInstanceState: Bundle?) {
1416
super.onCreate(savedInstanceState)
1517
setContentView(R.layout.activity_guest_main)
1618

17-
val adapter = ArrayAdapter(this, R.layout.simple_list_item_1, arrayList)
18-
listView.adapter = adapter
19+
val linearLayoutManager = LinearLayoutManager(applicationContext)
20+
recyclerView.layoutManager = linearLayoutManager
21+
val customAdapter = CustomAdapter(personNames, personImages, this)
22+
recyclerView.adapter = customAdapter
1923

2024
simpleSwipeRefreshLayout.setOnRefreshListener {
2125
simpleSwipeRefreshLayout.isRefreshing = false
@@ -24,9 +28,10 @@ class GuestMain : AppCompatActivity() {
2428
}
2529

2630
private fun shuffleItems(){
27-
arrayList.shuffle(Random(System.currentTimeMillis()))
28-
val adapter = ArrayAdapter(this, R.layout.simple_list_item_1, arrayList)
29-
listView.adapter = adapter
31+
personNames.shuffle(Random(System.currentTimeMillis()))
32+
personImages.shuffle(Random(System.currentTimeMillis()))
33+
val customAdapter = CustomAdapter(personNames, personImages, this)
34+
recyclerView.adapter = customAdapter
3035
}
3136

3237
}

app/src/main/res/layout/activity_guest_main.xml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@
1010
android:layout_width="match_parent"
1111
android:layout_height="match_parent">
1212

13-
<LinearLayout
13+
<androidx.recyclerview.widget.RecyclerView
14+
android:id="@+id/recyclerView"
1415
android:layout_width="match_parent"
15-
android:layout_height="match_parent"
16-
android:orientation="horizontal"
17-
android:padding="20dp">
18-
19-
<ListView
20-
android:id="@+id/listView"
21-
android:layout_width="match_parent"
22-
android:layout_height="match_parent" />
23-
</LinearLayout>
16+
android:layout_height="match_parent" />
2417

2518
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
2619
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="horizontal"
6+
android:padding="5dp">
7+
8+
<ImageView
9+
android:id="@+id/image"
10+
android:layout_width="70dp"
11+
android:layout_height="70dp"
12+
android:scaleType="fitXY"
13+
android:src="@mipmap/ic_launcher" />
14+
<TextView
15+
android:id="@+id/name"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_gravity="center_vertical"
19+
android:layout_marginLeft="10dp"
20+
android:text="ABCD"
21+
android:textColor="#000"
22+
android:textSize="20sp" />
23+
</LinearLayout>

app/src/main/res/layout/simple_list_item_1.xml

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)