Thursday 2 April 2015

RecyclerView Tutorial


Using RecyclerView, we have to be mind about two important aspects i.e
   
    1. RecyclerView.ViewHolder
    2. RecyclerView.Adapter
 

  My DemoApp Structure goes like this :




We will be using android Studio here.


    And Few properties of RecyclerView which i will discuss after code.
 
    So you might have to write something like this to run RecyclerView.

1. RecyclerViewActivity.java

public class RecyclerViewActivity extends Activity {

    ArrayList<String>mListitems;
    RecyclerView mRecyclerView;

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

    private void initiView(){
        mListitems=new ArrayList<String>();
        for(int i=0;i<125;i++){
            mListitems.add("List Item "+ i);
        }
        mRecyclerView=(RecyclerView) findViewById(R.id.recyclervew);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

       //The LinearLayoutManager is currently the only default implementation of LayoutManager. You can use this class to create either vertical or horizontal lists.

        LinearLayoutManager layoutManager = new LinearLayoutManager(RecyclerViewActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
 
       //Setting up ItemAnimator here
       mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        //setting up the adapter
        RecyclerViewAdapter mAdapter=new RecyclerViewAdapter(RecyclerViewActivity.this,mListitems);
        mRecyclerView.setAdapter(mAdapter);
    }

}


2. RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewViewHolder> {

      Context mContext;
      ArrayList<String>mListItems;
        public  RecyclerViewAdapter(Context mContext,ArrayList<String>mListItems){
            this.mContext=mContext;
            this.mListItems=mListItems;
        }


    @Override
    public RecyclerViewViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.row_recyclerview, viewGroup, false);
        return new RecyclerViewViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerViewViewHolder recyclerViewViewHolder, int i) {

        recyclerViewViewHolder.txtView.setText(mListItems.get(i));
    }

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


3. RecyclerViewViewHolder.java

public class RecyclerViewViewHolder extends RecyclerView.ViewHolder {

public TextView txtView;

    public RecyclerViewViewHolder(View itemView) {
        super(itemView);
        txtView = (TextView) itemView.findViewById(R.id.title);
    }
}


XML files now : 
1. activity_main.xml

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:id="@+id/recyclervew"         android:layout_width="match_parent"         android:layout_height="wrap_content"        /> </LinearLayout>

2. row_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent" android:layout_height="match_parent">     <TextView         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/txtview"         android:text="Demo recyclerView"         android:padding="10dp"         android:textColor="@android:color/holo_red_dark"         /> </LinearLayout>

Most important file now 
In App folder : build.gradle
Starts from here :>>>>

apply plugin: 'com.android.application'


android {     compileSdkVersion 22     buildToolsVersion "21.1.2"
    defaultConfig {         applicationId "com.example.kamalvaid.recyclerview"         minSdkVersion 14         targetSdkVersion 21         versionCode 1         versionName "1.0"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }
dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:21.0.0'     compile 'com.android.support:recyclerview-v7:21.0.0'
}




    Now there are using few properties listed below those purpose should be clear

    1. mRecyclerView.setHasFixedSize(true); => use this setting to improve performance if you know that changes in content do not change the layout size of the RecyclerView

    2. LinearlayoutManager => The LinearLayoutManager is currently the only default implementation of LayoutManager. You can use this class to create either vertical or horizontal lists.
    
       And we are using like this

    LinearLayoutManager layoutManager = new LinearLayoutManager(RecyclerViewActivity.this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);            
    layoutManager.setOrientation(LinearLayoutManager.Horizontal);


Also RecyclerView doesn't include Divider property by default, So we have to customize it .


Output : 

      




Happy Coding!!!
Cheers

    

    

No comments:

Post a Comment