Skip to content

Commit 32b8ba1

Browse files
committed
android recyclerview with listadapter
android recyclerview with listadapter
1 parent 859e2fe commit 32b8ba1

File tree

15 files changed

+565
-3
lines changed

15 files changed

+565
-3
lines changed

android-recyclerview/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Android | Create a List with RecyclerView
22
=========================================
33

4-
> `app` written in Java
5-
> `app-kotlin` is the same as `app` but written in Kotlin
4+
- `app` written in Java
5+
- `app-kotlin` is the same as `app` but written in Kotlin
6+
67

78

89
![android-recyclerview-app-layout](http://hmkcode.github.io/images/android/android-recyclerview-app_layout.png)
@@ -21,4 +22,14 @@ We will build two versions of a simple app (one in Java and one in Kotlin) that
2122

2223
Files we need for this app are shown in the image below.
2324

24-
![android-recyclerview-app-files](http://hmkcode.github.io/images/android/android-recyclerview-app-files.png)
25+
![android-recyclerview-app-files](http://hmkcode.github.io/images/android/android-recyclerview-app-files.png)
26+
27+
28+
Android | RecyclerView Using Support Library ListAdapter
29+
========================================================
30+
31+
- `app-listadapter` building recyclerview with `ListAdapter`
32+
33+
![android-recyclerview-listadapter](http://hmkcode.github.io/images/android/android-recyclerview-listadapter.gif)
34+
35+
<code>ListAdapter</code> for <code>RecyclerView</code> has been introduced in <a href="https://developer.android.com/topic/libraries/support-library/revisions.html#27-1-0">Revision 27.1.0 Release of Support Library</a>. <code>ListAdapter</code> uses <code>DiffUtil</code> under the hood to compute list diffs on a background thread. This will help <code>RecyclerView</code> animate changes automatically with less work on the UI thread.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
6+
7+
8+
defaultConfig {
9+
applicationId "com.hmkcode"
10+
minSdkVersion 19
11+
targetSdkVersion 27
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
26+
compileOptions {
27+
targetCompatibility 1.8
28+
sourceCompatibility 1.8
29+
}
30+
}
31+
32+
dependencies {
33+
implementation fileTree(dir: 'libs', include: ['*.jar'])
34+
35+
implementation 'com.android.support:appcompat-v7:27.1.1'
36+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
37+
testImplementation 'junit:junit:4.12'
38+
implementation 'com.android.support:recyclerview-v7:27.1.1'
39+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
40+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hmkcode">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".activities.MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.hmkcode.activities;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.support.v7.recyclerview.extensions.ListAdapter;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
import com.hmkcode.R;
9+
import com.hmkcode.adapters.MyListAdapter;
10+
import com.hmkcode.models.Link;
11+
import com.hmkcode.utils.LinkDiffCallback;
12+
import java.util.LinkedList;
13+
import java.util.List;
14+
15+
public class MainActivity extends AppCompatActivity {
16+
17+
private RecyclerView recyclerView;
18+
private List<Link> links;
19+
private ListAdapter listAdapter;
20+
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_main);
25+
recyclerView = findViewById(R.id.recyclerView);
26+
27+
// layout manager
28+
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
29+
recyclerView.setLayoutManager(layoutManager);
30+
31+
recyclerView.setClickable(true);
32+
33+
// list adapter
34+
links = getListData();
35+
36+
listAdapter = new MyListAdapter(new LinkDiffCallback(),
37+
link -> updateList(link));
38+
recyclerView.setAdapter(listAdapter);
39+
40+
listAdapter.submitList(getListData());
41+
42+
}
43+
44+
private void updateList(Link link){
45+
boolean removed = links.remove(link);
46+
listAdapter.submitList(new LinkedList(links));
47+
}
48+
49+
//generate a list of Link
50+
private List<Link> getListData(){
51+
List<Link> links = new LinkedList<Link>();
52+
53+
Link link = new Link();
54+
link.setIcon(R.drawable.ic_link);
55+
link.setTitle("HMKCODE BLOG");
56+
link.setUrl("hmkcode.com");
57+
58+
links.add(link);
59+
60+
link = new Link();
61+
link.setIcon(R.drawable.ic_twitter);
62+
link.setTitle("@HMKCODE");
63+
link.setUrl("twitter.com/hmkcode");
64+
65+
links.add(link);
66+
67+
link = new Link();
68+
link.setIcon(R.drawable.ic_github);
69+
link.setTitle("HMKCODE");
70+
link.setUrl("github.com/hmkcode");
71+
72+
links.add(link);
73+
return links;
74+
}
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.hmkcode.adapters;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.v7.recyclerview.extensions.ListAdapter;
5+
import android.support.v7.util.DiffUtil;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.ImageView;
11+
import android.widget.TextView;
12+
import com.hmkcode.R;
13+
import com.hmkcode.listeners.MyItemClickListener;
14+
import com.hmkcode.models.Link;
15+
16+
public class MyListAdapter extends ListAdapter<Link, MyListAdapter.MyViewHolder> {
17+
18+
MyItemClickListener myItemClickListener;
19+
public MyListAdapter(@NonNull DiffUtil.ItemCallback diffCallback,
20+
MyItemClickListener myItemClickListener) {
21+
super(diffCallback);
22+
this.myItemClickListener = myItemClickListener;
23+
}
24+
25+
@NonNull
26+
@Override
27+
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
28+
View itemLayoutView = LayoutInflater.from(parent.getContext())
29+
.inflate(R.layout.item_layout, null);
30+
31+
return new MyViewHolder(itemLayoutView);
32+
}
33+
34+
@Override
35+
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
36+
myViewHolder.bind(getItem(position));
37+
}
38+
39+
// inner class
40+
public class MyViewHolder extends RecyclerView.ViewHolder {
41+
42+
private View itemLayoutView;
43+
private TextView itemTitle;
44+
private TextView itemUrl;
45+
private ImageView itemIcon;
46+
47+
public MyViewHolder(View itemLayoutView) {
48+
super(itemLayoutView);
49+
this.itemLayoutView = itemLayoutView;
50+
this.itemTitle = itemLayoutView.findViewById(R.id.item_title);
51+
this.itemUrl = itemLayoutView.findViewById(R.id.item_url);
52+
this.itemIcon = itemLayoutView.findViewById(R.id.item_icon);
53+
}
54+
public void bind(Link link){
55+
this.itemIcon.setImageResource(link.getIcon());
56+
this.itemTitle.setText(link.getTitle());
57+
this.itemUrl.setText(link.getUrl());
58+
59+
this.itemLayoutView.setOnClickListener(
60+
v -> myItemClickListener.onClick(link));
61+
62+
}
63+
64+
}
65+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hmkcode.listeners;
2+
3+
import com.hmkcode.models.Link;
4+
5+
@FunctionalInterface
6+
public interface MyItemClickListener {
7+
public void onClick(Link link);
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.hmkcode.models;
2+
3+
public class Link {
4+
5+
private int icon;
6+
private String title;
7+
private String url;
8+
9+
public int getIcon() {
10+
return icon;
11+
}
12+
13+
public void setIcon(int icon) {
14+
this.icon = icon;
15+
}
16+
17+
public String getTitle() {
18+
return title;
19+
}
20+
21+
public void setTitle(String title) {
22+
this.title = title;
23+
}
24+
25+
public String getUrl() {
26+
return url;
27+
}
28+
29+
public void setUrl(String url) {
30+
this.url = url;
31+
}
32+
33+
@Override
34+
public boolean equals(Object obj) {
35+
return this.getUrl().equals(((Link)obj).getUrl());
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "Link{" +
41+
"icon=" + icon +
42+
", title='" + title + '\'' +
43+
", url='" + url + '\'' +
44+
'}';
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hmkcode.utils;
2+
3+
import android.support.v7.util.DiffUtil;
4+
import com.hmkcode.models.Link;
5+
6+
public class LinkDiffCallback extends DiffUtil.ItemCallback<Link> {
7+
@Override
8+
public boolean areItemsTheSame(Link oldItem, Link newItem) {
9+
return oldItem.getUrl().equals(newItem.getUrl());
10+
}
11+
12+
@Override
13+
public boolean areContentsTheSame(Link oldItem, Link newItem) {
14+
return oldItem.getUrl().equals(newItem.getUrl());
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<vector android:height="24dp" android:viewportHeight="1024"
2+
android:viewportWidth="1024" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#1B1F23" android:fillType="evenOdd" android:pathData="M512,0C229.12,0 0,229.12 0,512C0,738.56 146.56,929.92 350.08,997.76C375.68,1002.24 385.28,986.88 385.28,973.44C385.28,961.28 384.64,920.96 384.64,878.08C256,901.76 222.72,846.72 212.48,817.92C206.72,803.2 181.76,757.76 160,745.6C142.08,736 116.48,712.32 159.36,711.68C199.68,711.04 228.48,748.8 238.08,764.16C284.16,841.6 357.76,819.84 387.2,806.4C391.68,773.12 405.12,750.72 419.84,737.92C305.92,725.12 186.88,680.96 186.88,485.12C186.88,429.44 206.72,383.36 239.36,347.52C234.24,334.72 216.32,282.24 244.48,211.84C244.48,211.84 287.36,198.4 385.28,264.32C426.24,252.8 469.76,247.04 513.28,247.04C556.8,247.04 600.32,252.8 641.28,264.32C739.2,197.76 782.08,211.84 782.08,211.84C810.24,282.24 792.32,334.72 787.2,347.52C819.84,383.36 839.68,428.8 839.68,485.12C839.68,681.6 720,725.12 606.08,737.92C624.64,753.92 640.64,784.64 640.64,832.64C640.64,901.12 640,956.16 640,973.44C640,986.88 649.6,1002.88 675.2,997.76C877.44,929.92 1024,737.92 1024,512C1024,229.12 794.88,0 512,0Z"/>
4+
</vector>

0 commit comments

Comments
 (0)