Skip to content

Commit 4fdfe65

Browse files
author
Sothearith Sreang
committed
Implements GitHub Search Adapter for RecyclerView
1 parent 326dc75 commit 4fdfe65

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package thearith.github.com.github_search.view.adapter
2+
3+
import android.support.v7.widget.RecyclerView
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.TextView
7+
import com.astro.astro.views.utils.inflate
8+
import thearith.github.com.github_search.R
9+
import thearith.github.com.github_search.data.search.network.search.model.GitHubSearchItemModel
10+
11+
/**
12+
* Created by Thearith on 11/1/17.
13+
*/
14+
class GitHubSearchAdapter() :
15+
RecyclerView.Adapter<GitHubSearchAdapter.GitHubSearchViewHolder>() {
16+
17+
private var mData : MutableList<GitHubSearchItemModel> = mutableListOf()
18+
19+
fun addData(data : List<GitHubSearchItemModel>?) {
20+
if(data?.isNotEmpty() == true) {
21+
val toAddPos = mData.size
22+
val addedSize = data.size
23+
mData.addAll(data)
24+
25+
notifyItemRangeInserted(toAddPos, addedSize)
26+
}
27+
}
28+
29+
fun clearAll() {
30+
val size = mData.size
31+
mData.clear()
32+
33+
notifyItemRangeRemoved(0, size)
34+
}
35+
36+
override fun getItemCount() = mData.size
37+
38+
override fun onBindViewHolder(holder: GitHubSearchViewHolder?, position: Int) {
39+
val data = mData.get(position)
40+
41+
holder?.tvGitHubRepoName?.setText(data.fullName)
42+
holder?.tvGitHubRepoDescription?.setText(data.description)
43+
holder?.tvGitHubRepoLastUpdated?.setText(data.updatedAt)
44+
holder?.tvGitHubRepoStar?.setText(data.stars.toString())
45+
}
46+
47+
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) : GitHubSearchViewHolder {
48+
val view = parent?.inflate(R.layout.item_search)
49+
return GitHubSearchViewHolder(view)
50+
}
51+
52+
class GitHubSearchViewHolder(itemView : View?) : RecyclerView.ViewHolder(itemView) {
53+
val tvGitHubRepoName : TextView
54+
by lazy { itemView?.findViewById(R.id.tv_github_repo_name) as TextView }
55+
56+
val tvGitHubRepoDescription : TextView
57+
by lazy { itemView?.findViewById(R.id.tv_github_description) as TextView }
58+
59+
val tvGitHubRepoLastUpdated : TextView
60+
by lazy { itemView?.findViewById(R.id.tv_github_last_updated) as TextView }
61+
62+
val tvGitHubRepoStar : TextView
63+
by lazy { itemView?.findViewById(R.id.tv_github_repo_star) as TextView }
64+
}
65+
66+
}
538 Bytes
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:paddingTop="@dimen/search_item_padding"
6+
android:paddingBottom="@dimen/search_item_padding">
7+
8+
<LinearLayout
9+
android:id="@+id/ll_star"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_alignParentRight="true"
13+
android:orientation="vertical"
14+
android:paddingLeft="@dimen/search_item_spacing_small"
15+
android:paddingRight="@dimen/search_item_spacing_small">
16+
17+
<ImageView
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:src="@drawable/ic_star"
21+
android:layout_marginRight="@dimen/search_item_spacing_small"
22+
android:layout_gravity="center_horizontal"/>
23+
24+
<TextView
25+
android:id="@+id/tv_github_repo_star"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:textSize="12sp" />
29+
30+
</LinearLayout>
31+
32+
<LinearLayout
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:orientation="vertical"
36+
android:layout_toLeftOf="@id/ll_star">
37+
38+
<TextView
39+
android:id="@+id/tv_github_repo_name"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:textAppearance="@style/TextAppearance.AppCompat.Large"
43+
android:textStyle="bold"
44+
android:textColor="@color/colorGitHubRepoName"
45+
android:layout_marginBottom="@dimen/search_item_spacing_medium"/>
46+
47+
<TextView
48+
android:id="@+id/tv_github_description"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:textAppearance="@style/TextAppearance.AppCompat.Small"
52+
android:layout_marginBottom="@dimen/search_item_spacing_large"/>
53+
54+
<TextView
55+
android:id="@+id/tv_github_last_updated"
56+
android:layout_width="wrap_content"
57+
android:layout_height="wrap_content"
58+
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
59+
60+
</LinearLayout>
61+
62+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<declare-styleable name="LogoWithTextViewAttrs">
5+
<attr name="background" format="reference" />
6+
<attr name="text" format="string" />
7+
</declare-styleable>
8+
9+
</resources>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
4+
<!--Theme-->
35
<color name="colorPrimary">#3F51B5</color>
46
<color name="colorPrimaryDark">#303F9F</color>
57
<color name="colorAccent">#FF4081</color>
8+
9+
<!--GitHub Search Item-->
10+
<color name="colorGitHubRepoName">#2759D9</color>
11+
<color name="colorDivider">#e3e3e3</color>
12+
613
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<dimen name="search_item_padding">15dp</dimen>
5+
<dimen name="search_item_spacing_small">3dp</dimen>
6+
<dimen name="search_item_spacing_medium">5dp</dimen>
7+
<dimen name="search_item_spacing_large">10dp</dimen>
8+
9+
</resources>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<resources>
22
<string name="app_name">GitHub-Search</string>
3+
4+
<string name="message_welcome">Welcome to GitHub Search.\n Search your favorite repo</string>
5+
<string name="message_empty_result">Search result is empty</string>
6+
<string name="message_error">Oops! Something went wrong.\nPlease try again</string>
7+
8+
<string name="search_result_repo_count">%d repository counts</string>
9+
310
</resources>

0 commit comments

Comments
 (0)