Skip to content

Commit

Permalink
Adapter added
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanBh committed Jan 31, 2017
1 parent 3005cb4 commit aa928b9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ArrayList<MovieClass> myMovies;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView movieList= (RecyclerView) findViewById(R.id.my_recycler_view);


}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.sdsmdg.hareshkh.lectureassignment;

public class movies {
public class MovieClass {

String title;
String year;
String imageResId;
private String title;
private String year;
private String imageResId;

public movies(String title, String year, String imageResId) {
public MovieClass(String title, String year, String imageResId) {
this.title = title;
this.year = year;
this.imageResId = imageResId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.sdsmdg.hareshkh.lectureassignment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> {

private List<MovieClass> movies;
private Context ctx;

public MoviesAdapter(List<MovieClass> movies, Context ctx) {
this.movies = movies;
this.ctx = ctx;
}

public Context getCtx() {
return ctx;
}

public class ViewHolder extends RecyclerView.ViewHolder {

public TextView movieName;
public TextView movieYear;
public ImageView movieImage;

public ViewHolder(View itemView) {
super(itemView);
movieName= (TextView) itemView.findViewById(R.id.movie_name);
movieYear= (TextView) itemView.findViewById(R.id.movie_year);
movieImage= (ImageView) itemView.findViewById(R.id.movie_image);
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(getCtx());

View movieView= inflater.inflate(R.layout.row_layout,parent,false);

ViewHolder mHolder= new ViewHolder(movieView);
return mHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

MovieClass movie=movies.get(position);
holder.movieName.setText(movie.getTitle());
holder.movieYear.setText(movie.getYear());

Bitmap bitmap= BitmapFactory.decodeResource(getCtx().getResources(),

This comment has been minimized.

Copy link
@csoni111

csoni111 Feb 2, 2017

This is a very heavy task that you are doing here on main thread and that's why your RecyclerView hangs on scrolling.

There are two possible solutions here:

  1. Either do this on a separate thread or in an async task.
  2. Or you may use an image loading library to handle all this for you.
    Links: Picasso, Glide, Universal Image Loader.

All are great but I personally prefer Universal Image Loader.

Utils.getImageResourceFromString(movie.getImageResId(),getCtx()));
Bitmap bMapScaled=Utils.scaleToFitWidth(bitmap, (int) Utils.convertToPixel(500,getCtx()));

holder.movieImage.setImageBitmap(bMapScaled);

}

@Override
public int getItemCount() {
return movies.size();
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.sdsmdg.hareshkh.lectureassignment;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class Utils {

Expand All @@ -10,4 +14,14 @@ public static Bitmap scaleToFitWidth(Bitmap b, int width)
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
}

public static int getImageResourceFromString(String img_path, Context context){
int imageResource = context.getResources().getIdentifier(img_path, null, context.getPackageName());
return imageResource;
}

public static float convertToPixel(float myDPs,Context ctx){
Resources r = ctx.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, myDPs, r.getDisplayMetrics());
return px;
}
}
55 changes: 49 additions & 6 deletions app/src/main/res/layout/row_layout.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="16dp"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="8dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">

<android.support.v7.widget.CardView

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:scaleType="centerCrop"
android:id="@+id/movie_image" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="Movie"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/movie_name" />

<TextView
android:text="Year"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/movie_year" />


</LinearLayout>
</RelativeLayout>

</android.support.v7.widget.CardView>

0 comments on commit aa928b9

Please sign in to comment.