Android RecyclerView Tutorial

Step 1:
build.gradle
compile 'com.android.support:cardview-v7:26+'
compile 'com.android.support:recyclerview-v7:26.+'


Step 2:
main_activity.xml
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="csdevbin.arham.recyclerviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</RelativeLayout>
Step 3:
card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        card_view:cardCornerRadius="4dp">
                               
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">
            <TextView
                android:id="@+id/info_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="info"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/desc"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="desc" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>



 Step 4:
MainActivity.java  

package csdevbin.arham.recyclerviewdemo;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
   
private RecyclerView recyclerView;
   
private List<Item> itemList;
   
private RecyclerView.Adapter adapter;

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

       
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
       
recyclerView.setHasFixedSize(true);

        LinearLayoutManager linearLayoutManager =
new LinearLayoutManager(this);
       
recyclerView.setLayoutManager(linearLayoutManager);

        
itemList = new ArrayList<>();

       
for (int i = 0; i <= 10; i++) {
            Item item =
new Item(
                   
"Heading " + (i + 1),
                   
"desc"
           
);
           
itemList.add(item);
        }
       
adapter = new MyAdapter(itemList, this);
       
recyclerView.setAdapter(adapter);


    }
}


Step 5:


MyAdapter.java 

package csdevbin.arham.recyclerviewdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

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

   
private List<Item> itemList;
    Context
context;

   
public MyAdapter(List<Item> itemList, Context context) {
       
this.itemList = itemList;
       
this.context = context;
    }

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

   
@Override
   
public void onBindViewHolder(ViewHolder holder, int position) {
        Item item =
itemList.get(position);

        holder.
text_info.setText(item.getName());
        holder.
desc.setText(item.getDesc());


    }

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

   
public class ViewHolder extends RecyclerView.ViewHolder {

       
public TextView text_info;
       
public TextView desc;

       
public ViewHolder(View itemView) {
           
super(itemView);

           
text_info = (TextView) itemView.findViewById(R.id.info_text);
           
desc = (TextView) itemView.findViewById(R.id.desc);
        }
    }


}


Step 6:

Item.java

package csdevbin.arham.recyclerviewdemo;

/**
 * Created by Android on 8/28/2017.
 */

public class Item {
   
private String name;
   
private String desc;

   
public Item(String name, String desc) {
       
this.name = name;
       
this.desc = desc;
    }

   
public String getName() {
       
return name;
    }

   
public String getDesc() {
       
return desc;
    }
}








No comments

Powered by Blogger.