Saturday, April 2, 2016

RecyclerView Replacement of ListView in Android Lollipop - Part1

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of RecyclerView which is replacement of ListView in Android Lollipop.
In this example i displayed simple two text using RecyclerView.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 21 (Suggested)
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.

****** Please add dependencies in build.gradle file as 
I have added...
compile 'com.android.support:recyclerview-v7:23.1.1'

A.  


B. Layout xml code
a. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.exam.ravi.recyclerviewex1.MainActivity">

   <android.support.v7.widget.RecyclerView

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:id="@+id/recycler_view"

       android:scrollbars="vertical">
   </android.support.v7.widget.RecyclerView>
</RelativeLayout>

b. For Single Row of RecyclerView  row_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" android:layout_height="60dp">
    <TextView        android:layout_width="200dp"

        android:layout_height="match_parent"

        android:id="@+id/tx_state_name"

        android:text="Rajasthan"

        android:textAppearance="?android:textAppearanceLarge"

        android:gravity="left"

        android:layout_alignParentLeft="true"/>
    <TextView        android:layout_width="200dp"

        android:layout_height="match_parent"

        android:id="@+id/capital_name"

        android:text="Jaipur"

        android:textAppearance="?android:textAppearanceSmall"

        android:gravity="right"

       android:layout_toRightOf="@+id/tx_state_name"/>

</RelativeLayout>

C. Java Files 
a. MainActivity.java

package com.exam.ravi.recyclerviewex1;

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 {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    String states[]={"Rajasthan","Haryana","Punjab","MadhyaPradesh","Gujarat",
                    "HimachalPradesh","Jammu","AndraPradesh","Bihar","ChhattisGarh",
                    "Karanataka","Kerala"};
    String caps[]={"Jaipur","Chandigarh","Chandigarh","Bhopal","Ahmedabad",
                  "Shimla","SriNagar","Hyderabad","Patna","Raipur",
                  "Bengaluru","Thiruvananthapuram"};
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        adapter =  new MyRecyclerAdapter(states,caps);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }
}
b. MyRecyclerAdapter
package com.exam.ravi.recyclerviewex1;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyRecyclerAdapter extends 
        RecyclerView.Adapter<MyRecyclerAdapter.MyRecyclerViewHolder>
{
    String[] state_names,state_capitals;
    MyRecyclerAdapter(String[] stname, String[] capname)
    {
        state_names=stname;
        state_capitals=capname;
    }
    @Override

    public MyRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
        View view = LayoutInflater.from(parent.getContext()).inflate
                                     (R.layout.row_layout,parent,false);
        MyRecyclerViewHolder recyclerViewHolder =  new MyRecyclerViewHolder(view);
        return recyclerViewHolder;
    }
    @Override

    public void onBindViewHolder(MyRecyclerViewHolder holder, int position)
 {
        holder.Tx_state.setText(state_names[position]);
        holder.Tx_capital.setText(state_capitals[position]);
    }

    @Override    public int getItemCount() {
        return state_names.length;
    }
    public static class MyRecyclerViewHolder extends
                                        RecyclerView.ViewHolder
    {
        TextView Tx_state,Tx_capital;
        public MyRecyclerViewHolder(View itemView) {
            super(itemView);
            Tx_state = (TextView) itemView.findViewById(R.id.tx_state_name);
            Tx_capital = (TextView) itemView.findViewById(R.id.capital_name);
        }
    }
}
 

No comments:

Post a Comment