Skip to content

Commit 27ca3d0

Browse files
committed
Updates example with recyclerview
1 parent 62bfbcf commit 27ca3d0

File tree

10 files changed

+174
-16
lines changed

10 files changed

+174
-16
lines changed

com.vogella.android.github.issuetracker/app/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ android {
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
15+
def GITHUB_REPO = "GITHUB_REPO"
16+
def GITHUB_REPO_OWNER = "GITHUB_REPO_OWNER"
17+
def GITHUB_USER = "GITHUB_USER"
18+
def GITHUB_PW = "GITHUB_PW"
19+
def github_repo_owner = project.hasProperty('GITHUB_REPO_OWNER') ? project.property('GITHUB_REPO_OWNER') : ""
20+
def github_repo = project.hasProperty('GITHUB_REPO') ? project.property('GITHUB_REPO') : ""
21+
def github_user = project.hasProperty('GITHUB_USER') ? project.property('GITHUB_USER') : ""
22+
def github_password = project.hasProperty('GITHUB_PW') ? project.property('GITHUB_PW') : ""
23+
debug {
24+
buildConfigField "String", GITHUB_REPO_OWNER, "\"${github_repo_owner}\""
25+
buildConfigField "String", GITHUB_REPO, "\"${github_repo}\""
26+
buildConfigField "String", GITHUB_USER, "\"${github_user}\""
27+
buildConfigField "String", GITHUB_PW, "\"${github_password}\""
28+
}
1529
release {
1630
minifyEnabled false
1731
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1832
}
1933
}
34+
35+
dataBinding {
36+
enabled = true
37+
}
2038
}
2139

2240
dependencies {
@@ -30,5 +48,6 @@ dependencies {
3048
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
3149
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
3250
compile 'com.squareup.okhttp3:logging-interceptor:3.7.0'
51+
compile "com.android.support:recyclerview-v7:25.1.1"
3352
testCompile 'junit:junit:4.12'
3453
}

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/Issue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.vogella.android.github.issuetracker;
22

3-
class Issue {
3+
public class Issue {
44
public int id;
55
public String title;
6+
public User user;
67

78
@Override
89
public String toString() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
import java.util.List;
4+
5+
public class IssueAdapter extends MyBaseAdapter {
6+
private List<Issue> issues;
7+
8+
public IssueAdapter(List<Issue> issues) {
9+
this.issues = issues;
10+
}
11+
12+
@Override
13+
public Object getDataAtPosition(int position) {
14+
return issues.get(position);
15+
}
16+
17+
@Override
18+
public int getLayoutIdForType(int viewType) {
19+
return R.layout.rowlayout;
20+
}
21+
22+
@Override
23+
public int getItemCount() {
24+
return issues.size();
25+
}
26+
}

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/MainActivity.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5+
import android.support.v7.widget.LinearLayoutManager;
6+
import android.support.v7.widget.RecyclerView;
57
import android.support.v7.widget.Toolbar;
68
import android.view.KeyEvent;
79
import android.view.Menu;
@@ -11,6 +13,7 @@
1113
import android.widget.TextView;
1214
import android.widget.Toast;
1315

16+
import java.util.ArrayList;
1417
import java.util.List;
1518

1619
import io.reactivex.observers.DisposableSingleObserver;
@@ -19,11 +22,12 @@ public class MainActivity extends AppCompatActivity implements CredentialsDialog
1922

2023
EditText ownerEditText;
2124
EditText repositoryEditText;
22-
TextView issuesTextView;
25+
RecyclerView list;
26+
ArrayList<Issue> issues;
2327
private String password = "";
2428
private String username = "";
2529
private CommunicationController communicationController;
26-
30+
DisposableSingleObserver<List<Issue>> issuesObserver;
2731
@Override
2832
protected void onCreate(Bundle savedInstanceState) {
2933
super.onCreate(savedInstanceState);
@@ -33,6 +37,11 @@ protected void onCreate(Bundle savedInstanceState) {
3337

3438
ownerEditText = (EditText) findViewById(R.id.owner_edittext);
3539
repositoryEditText = (EditText) findViewById(R.id.repository_edittext);
40+
if (BuildConfig.DEBUG){
41+
ownerEditText.setText(BuildConfig.GITHUB_REPO_OWNER);
42+
repositoryEditText.setText(BuildConfig.GITHUB_REPO);
43+
}
44+
3645
repositoryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
3746
@Override
3847
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
@@ -43,7 +52,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
4352
return false;
4453
}
4554
});
46-
issuesTextView = (TextView) findViewById(R.id.issues_text);
55+
list = (RecyclerView) findViewById(R.id.list);
56+
list.setLayoutManager(new LinearLayoutManager(this));
57+
issues = new ArrayList<>();
58+
list.setAdapter(new IssueAdapter(issues));
4759

4860
communicationController = new CommunicationController();
4961
}
@@ -71,9 +83,10 @@ private DisposableSingleObserver<List<Issue>> getIssuesObserver() {
7183
return new DisposableSingleObserver<List<Issue>>() {
7284
@Override
7385
public void onSuccess(List<Issue> issues) {
74-
for (Issue issue : issues) {
75-
issuesTextView.append(issue.toString() + "\n");
76-
}
86+
IssueAdapter listAdapter = (IssueAdapter) list.getAdapter();
87+
MainActivity.this.issues.clear();
88+
MainActivity.this.issues.addAll(new ArrayList<Issue>(issues));
89+
list.getAdapter().notifyDataSetChanged();
7790
}
7891

7992
@Override
@@ -86,6 +99,10 @@ public void onError(Throwable e) {
8699
private void showCredentialsDialog() {
87100
CredentialsDialog dialog = new CredentialsDialog();
88101
Bundle arguments = new Bundle();
102+
if (BuildConfig.DEBUG && BuildConfig.GITHUB_USER.length() > 0){
103+
username = BuildConfig.GITHUB_USER;
104+
password = BuildConfig.GITHUB_PW;
105+
}
89106
arguments.putString("username", username);
90107
arguments.putString("password", password);
91108
dialog.setArguments(arguments);
@@ -100,13 +117,23 @@ public void onDialogPositiveClick(String username, String password) {
100117
}
101118

102119
private void queryForIssues() {
103-
issuesTextView.setText("");
104120
String owner = ownerEditText.getText().toString();
105121
String repository = repositoryEditText.getText().toString();
106-
122+
107123
if (username.isEmpty() || password.isEmpty()) {
108124
Toast.makeText(MainActivity.this, "Please provide your credentials", Toast.LENGTH_SHORT).show();
109125
return;
126+
if(!username.isEmpty() && !password.isEmpty() && !owner.isEmpty() && !repository.isEmpty()) {
127+
issuesObserver = getIssuesObserver();
128+
communicationController.loadIssues(username, password, issuesObserver , owner, repository);
129+
}
130+
}
131+
132+
@Override
133+
protected void onStop() {
134+
super.onStop();
135+
if (issuesObserver!=null && !issuesObserver.isDisposed()) {
136+
issuesObserver.dispose();
110137
}
111138

112139
if (owner.isEmpty() || repository.isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
import android.databinding.DataBindingUtil;
4+
import android.databinding.ViewDataBinding;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
public abstract class MyBaseAdapter extends RecyclerView.Adapter<MyBaseAdapter.MyViewHolder> {
11+
12+
// Provide a reference to the views for each data item
13+
// Complex data items may need more than one view per item, and
14+
// you provide access to all the views for a data item in a view holder
15+
public class MyViewHolder extends RecyclerView.ViewHolder {
16+
// each data item is just a string in this case
17+
private final ViewDataBinding binding;
18+
19+
public MyViewHolder(ViewDataBinding binding) {
20+
super(binding.getRoot());
21+
this.binding = binding;
22+
}
23+
public void bind(Object obj) {
24+
binding.setVariable(BR.obj,obj);
25+
binding.executePendingBindings();
26+
}
27+
}
28+
29+
@Override
30+
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
31+
// create a new view
32+
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
33+
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, getLayoutIdForType(viewType), parent, false);
34+
// set the view's size, margins, paddings and layout parameters
35+
return new MyViewHolder(binding);
36+
}
37+
38+
// Replace the contents of a view (invoked by the layout manager)
39+
@Override
40+
public void onBindViewHolder(MyViewHolder holder, int position) {
41+
holder.bind(getDataAtPosition(position));
42+
}
43+
44+
@Override
45+
public long getItemId(int position) {
46+
return View.NO_ID;
47+
}
48+
49+
public abstract Object getDataAtPosition(int position);
50+
51+
public abstract int getLayoutIdForType(int viewType);
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
public class User {
4+
public int id;
5+
public String login;
6+
7+
@Override
8+
public String toString() {
9+
return id + " - " + login;
10+
}
11+
}

com.vogella.android.github.issuetracker/app/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
android:maxLines="1" />
5454
</LinearLayout>
5555

56-
<TextView
57-
android:id="@+id/issues_text"
58-
android:layout_width="wrap_content"
59-
android:layout_height="wrap_content"
56+
<android.support.v7.widget.RecyclerView
57+
android:id="@+id/list"
58+
android:layout_width="match_parent"
59+
android:layout_height="match_parent"
6060
android:layout_marginLeft="8dp"
6161
android:layout_marginRight="8dp" />
6262
</LinearLayout>
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+
<layout xmlns:android="http://schemas.android.com/apk/res/android">
3+
<data>
4+
<variable name="obj" type="com.vogella.android.github.issuetracker.Issue"/>
5+
</data>
6+
<LinearLayout
7+
android:orientation="vertical"
8+
android:layout_width="match_parent"
9+
android:layout_height="?android:attr/listPreferredItemHeight">
10+
<TextView
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:textStyle="bold"
14+
android:text="@{obj.title}"/>
15+
<TextView
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:text="@{obj.user.login}"/>
19+
</LinearLayout>
20+
</layout>
21+

com.vogella.android.github.issuetracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.1'
8+
classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Apr 21 19:12:39 CEST 2017
1+
#Tue Apr 25 15:24:02 CEST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip

0 commit comments

Comments
 (0)