Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make RecyclerView in Android studio?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 392
    Comment on it

    Here, I have create a RecyclerView app in android .The RecyclerView is new advanced widget and flexible version of ListView.Below i have described step by step recycler view ..

    Step(1)- In (build.gradle) folder Add first dependencies .

     'com.android.support:recyclerview-v7:21.0.+'
    

    Step(2)-Create a new class(MyAdapter)

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private ArrayList<MyPojo> pojos;
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public View view;
            public ViewHolder(View v) {
                super(v);
                view = v;
            }
        }
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(ArrayList<MyPojo> pojos) {
            this.pojos = pojos;
        }
        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
             int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private ArrayList<MyPojo> pojos;
    
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class ViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public View view;
            public ViewHolder(View v) {
                super(v);
                view = v;
            }
        }
    
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(ArrayList<MyPojo> pojos) {
            this.pojos = pojos;
        }
    
        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row, parent, false);
            // set the view's size, margins, paddings and layout parameters
    
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
    
            TextView title =(TextView) holder.view.findViewById(R.id.title);
            TextView desc = (TextView) holder.view.findViewById(R.id.desc);
            final ImageView imageView =(ImageView)holder.view.findViewById(R.id.imageView);
            final ImageView imageView1=(ImageView)holder.view.findViewById(R.id.imageView2);
    
    
            title.setText(pojos.get(position).getTitle());
            desc.setText(pojos.get(position).getDesc());
            imageView.setImageResource(pojos.get(position).getImage());
            imageView1.setImageResource(pojos.get(position).getImage());
    
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                }
            });
        }
    
        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return pojos.size();
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            TextView title =(TextView) holder.view.findViewById(R.id.title);
            TextView desc = (TextView) holder.view.findViewById(R.id.desc);
            final ImageView imageView =(ImageView)holder.view.findViewById(R.id.imageView);
            final ImageView imageView1=(ImageView)holder.view.findViewById(R.id.imageView2);
            title.setText(pojos.get(position).getTitle());
            desc.setText(pojos.get(position).getDesc());
            imageView.setImageResource(pojos.get(position).getImage());
            imageView1.setImageResource(pojos.get(position).getImage());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return pojos.size();
        }
    }
    

    Step(3)- Making Layout XML(activity_main.xml)

    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
        <!-- A RecyclerView with some commonly used attributes -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#dbfffe" />
    </RelativeLayout>
    

    Step(4)-Create new Class for adding data (likename-Mypojo)

    public class MyPojo {
        String title;
        String desc;
        int image;
        public MyPojo(String title,String desc,int image){
            this.title =title;
            this.desc =desc;
            this.image =image;
        }
        public String getTitle(){
            return title;
        }
        public String getDesc(){
            return desc;
        }
        public int getImage(){
            return  image;
        }
    }
    

    Step(5)-Create a new class for making viewholder.

    public class RecyclerViewHolder extends LinearLayout {
        Context mContext;
        RecyclerView mLog;
        public RecyclerViewHolder(Context context)
        {
            super(context);
            mContext =context;
            setup();
        }
        public RecyclerViewHolder(Context context,AttributeSet attrs){
            super(context,attrs);
            mContext =context;
            setup();
        }
        private void setup(){
            LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.row, this);
        }
        public void setmLog(RecyclerView log){
            mLog =log;
            TextView tvTitle =(TextView)findViewById(R.id.title);
            TextView tvDescription=(TextView)findViewById(R.id.txtDescription);
            tvDescription.setText(mLog.getContentDescription()+"");
        }
    }
    

    Step(6)-Create a new row.xml layout .

    <?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="match_parent"
        android:padding="3dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/title"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:padding="2dp"
            android:id="@+id/desc"
            android:layout_below="@+id/title"
            android:layout_alignParentStart="true" />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:padding="2dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:padding="3dp"
            android:layout_below="@+id/desc"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
    

    Step(7)-MainActivity ..

    package com.tiwari.rajshekhar.recyclerviewdemo;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.media.Image;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    
    public class MainActivity extends Activity {
    
        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;
        RecyclerView my_recycler_view;
    
        Button btnAddItem;
        ListView lvList;
        ArrayList< MyPojo> myList = new ArrayList< MyPojo>();
        MyAdapter listAdapter;
        EditText etTitle, etDescription;
       String desc="";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
            // 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);
    
    
            // use a linear layout manager
            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mLayoutManager);
    
    
    
            ArrayList<MyPojo> pojos =new ArrayList<MyPojo>();
    
            for (int i=0;i<20;i++){
                pojos.add(new MyPojo("RAJ007"+i,"Evon-tec",
                        R.drawable.img1));
    
            }
    
            mAdapter = new MyAdapter(pojos);
            mRecyclerView.setAdapter(mAdapter);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: