GithubHelp home page GithubHelp logo

fajaragungpramana / section-recyclerview Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 1.0 153 KB

Library for android. RecyclerView adapter with header section

License: Apache License 2.0

Kotlin 100.00%
customview customcomponent customrecyclerview customrecyclerviewadapter customadapter sectionrecyclerview headerrecyclerview sectionheaderrecyclerview

section-recyclerview's Introduction

Android Section Recycler View

License

Library for android. RecyclerView adapter with header section

Installation

Add it in your root build.gradle at the end of repositories:

allProjects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add the dependency:

dependencies {
	implementation 'com.github.fajaragungpramana:section-recyclerview:0.0.2'
}

Usage

First step create layout that implement RecyclerView component.

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rv_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

then create your layout adapter, for example i create name_adapter.xml.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    tools:text="Name" />

then create data class that must extends class Section, for example i create data class named Name.

data class Name(
    val id: Int,
    val date: String,
    val listName: List<String>
) : Section(date)

on the class Section constructor fill with title you want to show in section list, in this case i use date variable as the title of section list.

then create class recycler view adapter for your layout adapter, for example i create class named NameAdapter.

class NameAdapter(private val listName: List<String>) :
    RecyclerView.Adapter<NameAdapter.ViewHolder>() {

    override fun getItemCount() = listName.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindName(listName[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.name_adapter,
                parent,
                false
            )
        )

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val mName by lazy { view.findViewById<TextView>(R.id.tv_name) }

        fun bindName(name: String) {
            mName.text = name
        }

    }

}

then create class recycler view adapter in this case i name MainAdapter but you must extends class SectionRecyclerViewAdapter, and you must extends class SectionRecyclerViewHolder for bind data to adapter and then inflate your custom adapter in the method bindSectionListAdapter. In this case i use NameAdapter.

class MainAdapter(private val listName: List<Name>) :
    SectionRecyclerViewAdapter<MainAdapter.ViewHolder, Name>(listName) {

    override fun viewHolder(view: View) = ViewHolder(view)

    inner class ViewHolder(view: View) : SectionRecyclerViewHolder(view) {

        override fun bindSectionListAdapter(recyclerView: RecyclerView, position: Int) {
            recyclerView.adapter = NameAdapter(listName[position].listName)
        }

    }

}

and in the last step. inflate your main adapter to main recycler view, in this case i use MainAdapter. Here is MainActivity with data dummy for example

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.rv_main)

        val listName = mutableListOf(
            Name(
                1,
                "01/01/2021",
                listOf("A", "B", "C", "D", "E")
            ),
            Name(
                2,
                "01/02/2021",
                listOf("F", "G", "H", "I", "J")
            ),
            Name(
                3,
                "01/03/2021",
                listOf("K", "L", "M", "N", "O")
            )
        )

        recyclerView.let {
            it.layoutManager = LinearLayoutManager(this)
            it.adapter = MainAdapter(listName)
        }

    }

}

Preview






























License

Copyright 2021 Fajar Agung Pramana

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

section-recyclerview's People

Contributors

fajaragungpramana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

praveenb

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.