Skip to content

Commit cb0d9d2

Browse files
committed
recyclerview upadated
recyclerview upadated
1 parent f5bf5fc commit cb0d9d2

31 files changed

+531
-205
lines changed

android-recyclerview/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Android | Create a List with RecyclerView
2+
=========================================
3+
4+
![android-recyclerview-app-layout]("http://hmkcode.github.io/images/android/android-recyclerview-app_layout.png" )
5+
6+
7+
Creating a scrollable list of elements is a common pattern in mobile application. Using RecyclerView we can list a large data sets or frequently changing one. `RecyclerView` is an advanced and flexible version of ListView, addressing serveral issues with existing listing views. Here we will build a simple application with `RecyclerView`
8+
9+
## Overview
10+
11+
We will build a simple app that lists a hard-coded `Link` in a `RecyclerView`. To display items on `RecyclerView` you need to have the following components:
12+
13+
- `RecyclerView` widget added to the activity layout.
14+
- A class extending `RecyclerView.Adapter`.
15+
- A class extending `RecyclerView.ViewHolder`.
16+
- Layout for RecyclerView items.
17+
18+
Files we need for this app are shown in the image below.
19+
20+
![android-recyclerview-app-files]({{ "http://hmkcode.github.io/images/android/android-recyclerview-app-files.png" | absolute_url }})

android-recyclerview/app/build.gradle

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 27
9+
defaultConfig {
10+
applicationId "com.hmkcode"
11+
minSdkVersion 19
12+
targetSdkVersion 27
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
implementation fileTree(dir: 'libs', include: ['*.jar'])
27+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28+
implementation 'com.android.support:appcompat-v7:27.1.1'
29+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
30+
implementation 'com.android.support:recyclerview-v7:27.1.1'
31+
testImplementation 'junit:junit:4.12'
32+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
33+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.hmkcode.android.recyclerview"
4-
android:versionCode="1"
5-
android:versionName="1.0" >
6-
7-
<uses-sdk
8-
android:minSdkVersion="8"
9-
android:targetSdkVersion="21" />
3+
package="com.hmkcode">
104

115
<application
126
android:allowBackup="true"
13-
android:icon="@drawable/ic_launcher"
7+
android:icon="@mipmap/ic_launcher"
148
android:label="@string/app_name"
15-
android:theme="@style/AppTheme" >
16-
<activity
17-
android:name=".MainActivity"
18-
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">
1913
<intent-filter>
2014
<action android:name="android.intent.action.MAIN" />
2115

2216
<category android:name="android.intent.category.LAUNCHER" />
2317
</intent-filter>
2418
</activity>
19+
2520
</application>
2621

27-
</manifest>
22+
</manifest>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.hmkcode.activities;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
7+
import com.hmkcode.R;
8+
import com.hmkcode.adapters.MyAdapter;
9+
import com.hmkcode.model.Link;
10+
11+
import java.util.LinkedList;
12+
import java.util.List;
13+
14+
public class MainActivity extends AppCompatActivity {
15+
16+
private RecyclerView recyclerView;
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_main);
21+
recyclerView = findViewById(R.id.recyclerView);
22+
23+
// layout manager
24+
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
25+
recyclerView.setLayoutManager(layoutManager);
26+
27+
// adapter
28+
RecyclerView.Adapter adapter = new MyAdapter(getListData());
29+
recyclerView.setAdapter(adapter);
30+
31+
}
32+
33+
//generate a list of Link
34+
private static List<Link> getListData(){
35+
List<Link> links = new LinkedList<Link>();
36+
37+
Link link = new Link();
38+
link.setIcon(R.drawable.ic_link);
39+
link.setTitle("HMKCODE BLOG");
40+
link.setUrl("hmkcode.com");
41+
42+
links.add(link);
43+
44+
link = new Link();
45+
link.setIcon(R.drawable.ic_twitter);
46+
link.setTitle("@HMKCODE");
47+
link.setUrl("twitter.com/hmkcode");
48+
49+
links.add(link);
50+
51+
link = new Link();
52+
link.setIcon(R.drawable.ic_github);
53+
link.setTitle("HMKCODE");
54+
link.setUrl("github.com/hmkcode");
55+
56+
links.add(link);
57+
return links;
58+
}
59+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.hmkcode.adapters;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.v7.widget.RecyclerView;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ImageView;
9+
import android.widget.TextView;
10+
11+
import com.hmkcode.R;
12+
import com.hmkcode.model.Link;
13+
14+
import java.util.List;
15+
16+
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
17+
18+
private List<Link> links;
19+
20+
// constructor
21+
public MyAdapter(List<Link> links){
22+
this.links = links;
23+
}
24+
25+
@NonNull
26+
@Override
27+
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
28+
29+
// inflate item_layout
30+
View itemLayoutView = LayoutInflater.from(parent.getContext())
31+
.inflate(R.layout.item_layout, null);
32+
33+
MyViewHolder vh = new MyViewHolder(itemLayoutView);
34+
return vh;
35+
}
36+
37+
@Override
38+
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
39+
holder.itemIcon.setImageResource(links.get(position).getIcon());
40+
holder.itemTitle.setText(links.get(position).getTitle());
41+
holder.itemUrl.setText(links.get(position).getUrl());
42+
}
43+
44+
@Override
45+
public int getItemCount() {
46+
if(links != null)
47+
return links.size();
48+
else
49+
return 0;
50+
}
51+
52+
// inner static class
53+
public static class MyViewHolder extends RecyclerView.ViewHolder {
54+
55+
public TextView itemTitle;
56+
public TextView itemUrl;
57+
public ImageView itemIcon;
58+
59+
public MyViewHolder(View itemLayoutView) {
60+
super(itemLayoutView);
61+
itemTitle = itemLayoutView.findViewById(R.id.item_title);
62+
itemUrl = itemLayoutView.findViewById(R.id.item_url);
63+
itemIcon = itemLayoutView.findViewById(R.id.item_icon);
64+
}
65+
}
66+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hmkcode.model;
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+
}
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)