Skip to content

Commit e97d06e

Browse files
author
Roger Hu
committed
Switch to using RecyclerView instead of ListView
1 parent 120b74a commit e97d06e

File tree

4 files changed

+70
-38
lines changed

4 files changed

+70
-38
lines changed

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ android {
2121
dependencies {
2222
testImplementation 'junit:junit:4.12'
2323
implementation 'com.android.support:appcompat-v7:27.1.1'
24+
implementation 'com.android.support:recyclerview-v7:27.1.1'
2425
implementation 'com.loopj.android:android-async-http:1.4.9'
2526
implementation 'com.github.bumptech.glide:glide:4.7.1'
2627
implementation group: 'com.google.guava', name: 'guava', version: '18.0'

app/src/main/java/com/codepath/debuggingchallenges/activities/MoviesActivity.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5-
import android.widget.ListView;
5+
import android.support.v7.widget.RecyclerView;
66

77
import com.codepath.debuggingchallenges.R;
88
import com.codepath.debuggingchallenges.adapters.MoviesAdapter;
@@ -22,21 +22,21 @@ public class MoviesActivity extends AppCompatActivity {
2222

2323
private static final String API_KEY = "a07e22bc18f5cb106bfe4cc1f83ad8ed";
2424

25-
ListView lvMovies;
25+
RecyclerView rvMovies;
2626
MoviesAdapter adapter;
2727
ArrayList<Movie> movies;
2828

2929
@Override
3030
protected void onCreate(Bundle savedInstanceState) {
3131
super.onCreate(savedInstanceState);
3232
setContentView(R.layout.activity_movies);
33-
lvMovies = (ListView) findViewById(R.id.lvMovies);
33+
rvMovies = findViewById(R.id.rvMovies);
3434

3535
// Create the adapter to convert the array to views
36-
MoviesAdapter adapter = new MoviesAdapter(this, movies);
36+
MoviesAdapter adapter = new MoviesAdapter(movies);
3737

3838
// Attach the adapter to a ListView
39-
lvMovies.setAdapter(adapter);
39+
rvMovies.setAdapter(adapter);
4040

4141
fetchMovies();
4242
}

app/src/main/java/com/codepath/debuggingchallenges/adapters/MoviesAdapter.java

+53-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.content.Context;
44
import android.content.res.Resources;
55
import android.graphics.Color;
6+
import android.support.annotation.NonNull;
7+
import android.support.v7.widget.RecyclerView;
68
import android.view.LayoutInflater;
79
import android.view.View;
810
import android.view.ViewGroup;
@@ -16,44 +18,72 @@
1618

1719
import java.util.List;
1820

19-
public class MoviesAdapter extends ArrayAdapter<Movie> {
21+
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> {
2022

21-
public MoviesAdapter(Context context, List<Movie> movies) {
22-
super(context, 0, movies);
23+
private List<Movie> movies;
24+
25+
public class ViewHolder extends RecyclerView.ViewHolder {
26+
// only needed because we need to set the background color
27+
View view;
28+
29+
// Lookup view for data population
30+
TextView tvName;
31+
TextView tvRating;
32+
ImageView ivPoster;
33+
34+
public ViewHolder(View itemView) {
35+
super(itemView);
36+
37+
view = itemView;
38+
tvName = itemView.findViewById(R.id.tvTitle);
39+
tvRating = itemView.findViewById(R.id.tvRating);
40+
ivPoster = itemView.findViewById(R.id.ivPoster);
41+
}
42+
}
43+
44+
public MoviesAdapter(List<Movie> movies) {
45+
this.movies = movies;
2346
}
2447

2548
@Override
26-
public View getView(int position, View convertView, ViewGroup parent) {
27-
// Get the data item for this position
28-
Movie movie = getItem(position);
29-
// Check if an existing view is being reused, otherwise inflate the view
30-
if (convertView == null) {
31-
convertView = LayoutInflater
32-
.from(getContext())
33-
.inflate(R.layout.item_movie, parent, false);
34-
}
35-
// Lookup view for data population
36-
TextView tvName = (TextView) convertView.findViewById(R.id.tvTitle);
37-
TextView tvRating = (TextView) convertView.findViewById(R.id.tvRating);
38-
ImageView ivPoster = (ImageView) convertView.findViewById(R.id.ivPoster);
49+
public int getItemCount() {
50+
return 0;
51+
}
52+
53+
@NonNull
54+
@Override
55+
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
56+
Context context = parent.getContext();
57+
LayoutInflater inflater = LayoutInflater.from(context);
58+
59+
// Inflate the custom layout
60+
View movieView = inflater.inflate(R.layout.item_movie, parent, false);
61+
62+
// Return a new holder instance
63+
ViewHolder viewHolder = new ViewHolder(movieView);
64+
return viewHolder;
65+
}
66+
67+
@Override
68+
public void onBindViewHolder(MoviesAdapter.ViewHolder viewHolder, int position) {
3969

70+
Movie movie = movies.get(position);
4071

4172
// Populate the data into the template view using the data object
42-
tvName.setText(movie.getTitle());
73+
viewHolder.tvName.setText(movie.getTitle());
4374

44-
Resources resources = getContext().getResources();
75+
Resources resources = viewHolder.tvName.getResources();
4576
double movieRating = movie.getRating();
4677

4778
if (movieRating > 6) {
48-
convertView.setBackgroundColor(Color.GREEN);
79+
viewHolder.view.setBackgroundColor(Color.GREEN);
4980
}
5081

5182
String ratingText = String.format(resources.getString(R.string.rating), movieRating);
52-
tvRating.setText(ratingText);
83+
viewHolder.tvRating.setText(ratingText);
5384

54-
Glide.with(getContext()).load(movie.getPosterUrl()).into(ivPoster);
85+
Glide.with(viewHolder.ivPoster.getContext()).load(movie.getPosterUrl()).into(
86+
viewHolder.ivPoster);
5587

56-
// Return the completed view to render on screen
57-
return convertView;
5888
}
5989
}
+11-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
tools:context=".activities.MoviesActivity">
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".activities.MoviesActivity">
78

8-
<ListView
9+
<android.support.v7.widget.RecyclerView
10+
android:id="@+id/rvMovies"
911
android:layout_width="match_parent"
1012
android:layout_height="match_parent"
11-
android:id="@+id/lvMovies"
12-
android:layout_alignParentTop="true"
13-
android:layout_alignParentLeft="true"
1413
android:layout_alignParentStart="true"
15-
android:layout_alignParentBottom="true"
14+
android:layout_alignParentLeft="true"
15+
android:layout_alignParentTop="true"
16+
android:layout_alignParentEnd="true"
1617
android:layout_alignParentRight="true"
17-
android:layout_alignParentEnd="true" />
18+
android:layout_alignParentBottom="true"/>
1819
</RelativeLayout>

0 commit comments

Comments
 (0)