Code Monkey home page Code Monkey logo

prettylistview's Introduction

PrettyListView

Powerful recycler view with the ability to swapToRefresh and pagination using NoPaginate library with few number of code lines and updating via diffUtils. Lets see How to use it :

Installation

  • Step 1. Add the JitPack repository to your build file

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  • Step 2. Add the dependency

	dependencies {
           implementation 'com.github.Tripl3Dev:PrettyListView:1.0.0'
   	}

Usage

1. In XML View

        <com.tripl3dev.prettyListView.PrettyList
        android:id="@+id/testListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

To Add SwapToRefresh to your view you have to add

        <com.tripl3dev.prettyListView.PrettyList
        app:hasSwapToRefresh="true" />

2. In Activity or Fragment

  • Get your view
       val prettyList =  findViewById<PrettyList>(R.id.testListView)
  • Intialize adapter instance
    mAdapter = BaseListAdapter(object :MainHolderInterface<TestModel>{
            //Mandatory
            override fun getView(type: Int): Int {
            return  R.layout.your_list_item_layout
            }
            //Mandatory
            override fun getViewData(holder: RecyclerView.ViewHolder
            , currentItem: TestModel, position: Int) {
                //yourItemView = holder.itemView
            }
            override fun getListCopy(): java.util.ArrayList<TestModel>? {
                return YourList
            }
            override fun getItemViewType(position: Int): Int {
                return YourItemTypes
            }
        },yourContext)
  • set up List
  1. you can get your list view then set its layout and other configuration u need
var myListView = prettyList.listView
myListView.layoutManager = LinearLayoutManager(this)
myListView.listView.setHasFixedSize(true)
myListView.listView.adapter = mAdapter
  1. Adding data to your list and notifyList You have three ways to notify list modification -- First : if you implement fun getListCopy(): java.util.ArrayList? then all what you need to do is to modify the list instance you return then mAdapter.updateList() -- Second : if you don't then u have to call mAdapter.updateList(yourNewListAfterModification) -- Third : You can use mAdapter.notify..() Methods
  • Set pagination config and callback to your list
  1. setOnLoadMoreListener
prettyList.setOnLoadMoreListener(object : PrettyList.OnLoadMoreListener {
            override fun onLoadMore(currentPage: Int) {
            // Load More Data Here
            }
        })
  1. set customErrorView to be displayed on pagination error
  prettyList.setPaginationErrorItem(PaginationErrorItem(R.layout.your_pagination_error_layout, object : PaginationErrorItem.PaginateErrorListener {
            override fun getErrorView(errorView: View) {
                // Get you your error view to make any action on it
                val button = errorView.findViewById<Button>(R.id.errorBut)
                button.setOnClickListener{retry()}
                }
            }
        }))
  1. setCustomLoadingView to be displayed on loading
  prettyList.setPaginationLoadingItem(PaginationLoadingItem(R.layout.paginate_loading_custom_layout, object : PaginationLoadingItem.PaginateLoading {
            override fun getLoadingView(loadingView: View) {
                // Get you your loading view to make any action on it
                }
        }))
  1. (very Important) Finally you have to commit pagination ,it wont work without commit prettyList.commitPagination()
  2. There are some methods for pagination to load, StopLoading, showError, hideError, setNoMoreItems and setFirstPage index(Should use before setting pagination default = 1 )
prettyList.showError(show: Boolean)
prettyList.showLoading(show: Boolean) 
prettyList.setNoMoreItems(noMoreItems: Boolean)
setFirstPage(firstPage: Int)
  1. You can reset pagination and set currentPage to the default prettyList.resetPagination()
  2. You should call onDestroyPrettyList() in the onDestroy of activity or fragment to avoid memory leak
  override fun onDestroy() {
        super.onDestroy()
        prettyList.onDestroyPrettyList()
    }
  • setting swapToRefresh Listener
prettyList.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener { 
//Implement your onRefresh action here
})

//You can stop refreshing by 
prettyList.stopRefreshing()

Credits

  1. NoPaginate

prettylistview's People

Contributors

rohyme avatar

Watchers

 avatar

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.