Easy Implementation Of Android Recycler View 2018

by ananta

In This Tutorial, We will Cover a Basic way to Implement Recycler View in Android

 

 

Our File Structures

  • MainActivity.java // Main/ Launcher Activity
  • MyAdapter // RecyclerView Adapter
  • Recipe // DataModel
  • Utils // Demo Recipes Data

MainActivity.java

package com.codse.ananta.vogella2;

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

public class MainActivity extends AppCompatActivity {

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

        RecyclerView recyclerView = findViewById(R.id.recyclerView);

        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
        MyAdapter adapter = new MyAdapter(getApplicationContext(),Utils.getRecipes());
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:app="//schemas.android.com/apk/res-auto"
    xmlns:tools="//schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.codse.ananta.vogella2.MainActivity">
 <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:id="@+id/recyclerView"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:app="//schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="//schemas.android.com/tools"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:src="@mipmap/ic_launcher_round"
         />

    <TextView
        android:id="@+id/txtFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="10sp"
        android:layout_marginLeft="20sp"
        android:layout_toEndOf="@+id/imageView"
        android:padding="5sp"
        android:textSize="20sp"
        tools:text="First Line"
        />

    <TextView
        android:id="@+id/txtSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtFirst"
        android:layout_marginLeft="20sp"
        android:layout_toEndOf="@+id/imageView"
        android:padding="5sp"
        tools:text="Second Line"
        />
</RelativeLayout>

MyAdapter.java

package com.codse.ananta.vogella2;

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

import org.w3c.dom.Text;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by ananta on 5/5/18.
 */
// A Generic Class That takes ViewHolder(RecyclerView Holder)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    public Context context;
    public ArrayList<Recipe> recipes;

    public MyAdapter(Context context, ArrayList<Recipe> arrayList){
        this.context = context;
        this.recipes = arrayList;

    }

    //Creates Views

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.row_layout,parent,false);
        return new ViewHolder(v);
    }
    //Binds Views
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Recipe recipe = recipes.get(position);
//        try{
//            URL url = new URL(recipe.getUrl());
//            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//            holder.imgView.setImageBitmap(bmp);
//        }catch(MalformedURLException mue){
//            Log.e("MyAdapter","Malformed URL exception");
//        }catch(IOException ex){
//            Log.e("MyAdapter","IOException exception");
//        }
        holder.txtFirst.setText(recipe.getTitle());
        holder.txtSecond.setText(recipe.getSubTitle());

    }

    @Override
    public int getItemCount() {
        return recipes.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView txtFirst;
        public ImageView imgView;
        public TextView txtSecond;
        public View layout;
        public ViewHolder(View itemView) {
            super(itemView);
        layout = itemView;
            txtFirst = (TextView) itemView.findViewById(R.id.txtFirst);
            txtSecond = (TextView) itemView.findViewById(R.id.txtSecond);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }
}

Recipe.java

package com.codse.ananta.vogella2;

/**l
 * Created by ananta on 5/5/18.
 */

public class Recipe {
    public String url;
    public String title;
    public String subTitle;

    public Recipe(String url, String title, String subTitle){
        this.url = url;
        this.title = title;
        this.subTitle = subTitle;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }
}

Utils.java

package com.codse.ananta.vogella2;

import java.util.ArrayList;

/**
 * Created by ananta on 5/5/18.
 */

public class Utils {
    public static ArrayList<Recipe> getRecipes(){
        ArrayList<Recipe> Recipes = new ArrayList<>();
        Recipes.add(new Recipe("//2117e.https.cdn.softlayer.net/802117E/www.archanaskitchen.com/images/archanaskitchen/1-Author/shaikh.khalid7-gmail.com/Chicken_Momos_Recipe_Delicious_Steamed_Chicken_Dumplings.jpg","Chicken Momo","Non-Veg"));
        Recipes.add(new Recipe("","Chicken Burger","Non-Veg"));
        Recipes.add(new Recipe("","Veg Chowmien","Veg"));
        Recipes.add(new Recipe("","Chicken Garlic","Non-Veg"));
        Recipes.add(new Recipe("","Buff Sausage","Non-Veg"));
        Recipes.add(new Recipe("","AAlu Stick","Veg"));
        Recipes.add(new Recipe("","Chicken Sausage","Non-Veg"));

        return Recipes;
    };
}


        
Tagged
Next Article

Leave a Reply

Your email address will not be published.
*
*