Android App Development With AndroidCource: grid layout in recycler view
Showing posts with label grid layout in recycler view. Show all posts
Showing posts with label grid layout in recycler view. Show all posts

Tuesday, 29 August 2017

RecyclerView Staggered Layout Example in Android

Add Library into build.gradle Using app folder

    compile 'com.android.support:recyclerview-v7:26+'
MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.recyclerviewstaggeredlayout.MainActivity">

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

</LinearLayout>
item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java

package com.example.bhaumik.recyclerviewstaggeredlayout;

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

public class MainActivity extends AppCompatActivity {

    MyAdapter adapter;

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

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

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));

        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
    }
}
MyAdapter.java

package com.example.bhaumik.recyclerviewstaggeredlayout;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by Bhaumik on 8/29/2017.
 */

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

    int img[] = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};


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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);

        return new MyHolder(view);
    }

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

        holder.imageView.setImageResource(img[position]);

    }

    @Override
    public int getItemCount() {
        return img.length;
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        ImageView imageView;

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

            imageView = itemView.findViewById(R.id.image_view);
        }
    }
}

Ads