Android App Development With AndroidCource: how to create recycler view
Showing posts with label how to create recycler view. Show all posts
Showing posts with label how to create recycler view. Show all posts

Tuesday, 22 August 2017

Recycler View Example in Android

RecyclerViewActivity.xml
 
 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  android:orientation="vertical"
                  tools:context="com.example.bhaumik.recyclerviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view"
            android:scrollbars="vertical" />
</LinearLayout>
Note : You Can Use Card View else any layout to use this Xml.
Row_Item.xml
<?xml version="1.0" encoding="utf-8"?>
    < android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_margin="10dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="5dp"
            app:cardPreventCornerOverlap="true"
            app:cardBackgroundColor="@color/colorAccent">

      <TextView
        android:id="@+id/tv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:textSize="20dp"
        android:padding="5dp"
        android:textStyle="bold"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="Hello World"/>

</android.support.v7.widget.CardView>
RecyclerViewActivity.java
package com.bhaumik.programmingstudy;
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;

public class MainActivity extends AppCompatActivity {

    ArrayList< String> version_names;
    RecyclerView recyclerView;

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

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

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);

        version_names = new ArrayList<>();

        version_names.add("Aestro");
        version_names.add("Blender");
        version_names.add("CupCake");
        version_names.add("Donut");
        version_names.add("Eclair");
        version_names.add("Froyo");
        version_names.add("GingerBread");
        version_names.add("HonyComb");
        version_names.add("Ice Cream Sendwich");
        version_names.add("JellyBean");
        version_names.add("Kitkat");
        version_names.add("Lolipop");
        version_names.add("Marshmallow");
        version_names.add("Noght");

        VersionAdapter versionAdapter = new VersionAdapter(version_names,MainActivity.this);
        recyclerView.setAdapter(versionAdapter);

    }
}
VersionAdapter.java
  
package com.pstudy.bhaumik.programmingstudy;

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 android.widget.Toast;

import java.util.ArrayList;


public class VersionAdapter extends RecyclerView.Adapter<VersionAdapter.MyViewHolders>{

    ArrayList<String> list_version;
    Context context;

    public VersionAdapter(ArrayList<String> list_version, Context context) {
        this.list_version = list_version;
        this.context = context;
    }

    @Override
    public MyViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View view  = LayoutInflater.from(context).inflate(R.layout.row_item,parent,false);

        MyViewHolders myViewHolders = new MyViewHolders(view);

        return myViewHolders;
    }

    @Override
    public void onBindViewHolder(final MyViewHolders holder, final int position) {

        holder.textView.setText(list_version.get(position));

        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"Select Version : " + list_version.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    public static class MyViewHolders extends RecyclerView.ViewHolder{

        TextView textView;


        public MyViewHolders(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }
}
Note : Add Library in build.gradle -> app file and copy and paste dependancy in build.gradle file compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1'

Ads