GithubHelp home page GithubHelp logo

loader-recyclerview's Introduction

Sample Loader for RecyclerView

Loader sample using network state and custom adapter, work with endless recyclerview listener and paging library.

  • Edit your adapter like this
    /**
     * use network state
     * */
    private var networkState: NetworkState? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType) {
            R.layout.item_view -> MainViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false))
            R.layout.item_loader -> NetworkViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_loader, parent, false))
            else  -> throw IllegalArgumentException("not found view holder")
        }
    }

    /**
     * if has extra row, size + 1 for loader
     */
    override fun getItemCount(): Int = 
            list.size + if (hasExtraRow()) 1 else 0

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            R.layout.item_view -> {
                // bind to your main holder
            }
            R.layout.item_loader -> {
                // bind to your network holder
            }
        }
    }

    /**
     * detect extra row
     * */
    private fun hasExtraRow() = 
            networkState != null && networkState != NetworkState.LOADED

    /**
     * set view type,
     * if has extra row, viewType is loader
     * */
    override fun getItemViewType(position: Int): Int {
        return if (hasExtraRow() && position == itemCount -1 ) {
            R.layout.item_loader
        } else {
            R.layout.item_view
        }
    }

    /**
     * set network state from activity / fragment
     * */
    fun setNetworkState(newNetworkState: NetworkState?) {
        val previousState = this.networkState
        val hadExtraRow = hasExtraRow()
        this.networkState = newNetworkState
        val hasExtraRow = hasExtraRow()
        if (hadExtraRow != hasExtraRow) {
            if (hadExtraRow) {
                notifyItemRemoved(itemCount)
            } else {
                notifyItemInserted(itemCount)
            }
        } else if (hasExtraRow && previousState != newNetworkState) {
            notifyItemChanged(itemCount - 1)
        }
    }

loader-recyclerview's People

Contributors

kucingapes avatar

Stargazers

irmansyah avatar Fransisco avatar

Watchers

James Cloos avatar

Forkers

syntaxxxxx

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.