Coder Social home page Coder Social logo

martinapinky / staggered-grid Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 3.0 3.77 MB

Android RecyclerView with Staggered Grid Layout Manager

Home Page: https://medium.com/swlh/staggered-recyclerview-with-multiple-selection-in-kotlin-8eb37d5ba9e1

Kotlin 100.00%
staggered-grid-layout staggered-recycler-view recyclerview android-studio android-recyclerview

staggered-grid's Introduction

staggered-grid

Android RecyclerView with Staggered Grid Layout Manager Example

Getting Started

Create a new Android Studio project with the empty activity template. Add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.google.android.material:material:1.3.0-alpha02'
    implementation 'com.makeramen:roundedimageview:2.3.0'
}

XML

Add the following to your activity_main.xml file:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/postsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingStart="0dp"
        android:paddingEnd="15dp"
        android:paddingBottom="15dp" />

RecyclerView Item

Create an XML file named post_item_container.xml with the following:

<?xml version="1.0" encoding="utf-8"?>
<com.makeramen.roundedimageview.RoundedImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/imagePost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:layout_marginTop="15dp"
    android:adjustViewBounds="true"
    app:riv_corner_radius="12dp">
</com.makeramen.roundedimageview.RoundedImageView>

Kotlin

Create a data class called PostItem.kt with the following:

data class PostItem(var image: Int)

Create a RecyclerView Adapter called Posts Adapter with the following:


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.makeramen.roundedimageview.RoundedImageView

class PostsAdapter internal constructor(
    private val context: Context,
    private val postItems: List<PostItem>
) :
    RecyclerView.Adapter<PostsAdapter.ListViewHolder>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)

    inner class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val postImageView: RoundedImageView = itemView.findViewById(R.id.imagePost)

        fun setPostImage(postItem: PostItem) {
            postImageView.setImageResource(postItem.image)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
        val itemView = inflater.inflate(R.layout.post_item_container, parent, false)
        return ListViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
        holder.setPostImage(postItems[position])
    }

    override fun getItemCount(): Int {
        return postItems.size
    }


}

Add the following to your MainActivity.kt file:

val postsRecyclerView: RecyclerView = findViewById(R.id.postsRecyclerView)
        postsRecyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

        val postItems: MutableList<PostItem> = mutableListOf()
        postItems.add(PostItem(R.drawable.leeminho))
        postItems.add(PostItem(R.drawable.leejongsuk))
        postItems.add(PostItem(R.drawable.chaeunwoo))
        postItems.add(PostItem(R.drawable.seokangjoon))
        postItems.add(PostItem(R.drawable.kimsoohyun))
        postItems.add(PostItem(R.drawable.parkseojoon))
        postItems.add(PostItem(R.drawable.seoinguk))
        postItems.add(PostItem(R.drawable.jichangwook))
        postItems.add(PostItem(R.drawable.yooseungho))
        postItems.add(PostItem(R.drawable.leeseunggi))

        postsRecyclerView.adapter = PostsAdapter(this, postItems)

Screenshots

Screen Shot 1 Screen Shot 2

staggered-grid's People

Contributors

martinapinky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

staggered-grid's Issues

OnClick listener not working

hello, I am trying to add onClick action for items but doing so crashes the app.

tried on PostsAdapter
'''
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
holder.setPostImage(postItems[position])

    holder.postImageView.setOnClickListener { view ->
        Toast.makeText(view.context, position, Toast.LENGTH_SHORT).show()

    }
}

'''
help me with the solution.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.