Friday, 6 January 2017

Android Recycler GridView Example

Hello friends today i am going to share a good tutorial of recycler list view .Please follow me step by step ....

                

Donwload full source code
http://viid.me/qi0v4T
Step 1 : Create a project Recycler View Example with packagename com.ritesh.recyclerview and activity  MainActivity.java alongwith activity_main.xml layout

Step 2 : Open build.gradle and paste this code and sync your project

compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.1.0'


after adding this code your build.gradle look like this

apply plugin: 'com.android.application'

android {
 compileSdkVersion 25
 buildToolsVersion "25.0.2"
 defaultConfig {
 applicationId "com.ritesh.recyclerview"
 minSdkVersion 10
 targetSdkVersion 25
 versionCode 1
 versionName "1.0"
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
 exclude group: 'com.android.support', module: 'support-annotations'
 })
 compile 'com.android.support:appcompat-v7:25.1.0'
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:recyclerview-v7:25.1.0'
}

Step 3 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="colorPrimary">#3F51B5</color>
 <color name="colorPrimaryDark">#303F9F</color>
 <color name="colorAccent">#FF4081</color>
</resources>

Step 4 : dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>


Step 5 :your  styles.xml code like this

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>



Step 6 :your  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ritesh.recyclerview">

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>


Step 6 :Your complete MainActivity.java code like this

package com.ritesh.recyclerview;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

 ArrayList<String> list=new ArrayList<>();
 for (int i=0;i<40;i++) {
 list.add("Ritesh List Item "+i);
 }


 RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
 recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(MainActivity.this,3);
recyclerView.setLayoutManager(layoutManager); MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this,list); recyclerView.setAdapter(adapter); } public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { private ArrayList<String> list; private Context context; private int lastPosition = -1; public MyRecyclerAdapter(Context context, ArrayList<String> list1) { this.list = list1; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(MyRecyclerAdapter.ViewHolder viewHolder, final int i) { viewHolder.textView_name.setText(String.valueOf(list.get(i))); viewHolder.lnCover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,"Item Clicked "+i,Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ public RelativeLayout lnCover; public TextView textView_name; public ViewHolder(View view) { super(view); lnCover = (RelativeLayout) view .findViewById(R.id.lnCover); textView_name = (TextView) view .findViewById(R.id.textView_name); } } } }
Step 6 :Your activity_main.xml code like this

<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@android:color/white"
 >


 <android.support.v7.widget.RecyclerView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/recycler_view"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />
</RelativeLayout>


Step 7 :Your list_item.xml code like this
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:id="@+id/lnCover" android:paddingRight="8dp" android:background="@android:color/white" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_lock_lock" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="8dp" android:id="@+id/imageView2" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginLeft="15dp" android:layout_marginTop="8dp" android:textColor="@android:color/black" android:textSize="17sp" android:layout_below="@+id/imageView2" android:id="@+id/textView_name" /> </RelativeLayout>

Step 7 : Run your project

No comments:

Post a Comment