Giter Site home page Giter Site logo

mecofarid / dynamicspinner Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 6.49 MB

Dyanmic spinner with searchable spinner item and hierarchical selection

Kotlin 82.68% Java 17.32%
spinner spinners spinner-components spinner-custom spinnerview spinnerlikeinandroid searchable-spinner searchablespinner hierarchicspinner hierarchic-spinner

dynamicspinner's Introduction

Description

DynamicSpinner is a library content of each DynamicSpinner changes depending on the previous selection. You can find the demo application apk at the releases tab.

Reason for development

Probably, you're familiar with this nested list where each item has their own subcategory and we have to filter what subcategory is shown depending on what is selected in previous (parent) selection.

Demo

Demo application apk at the releases tab.

Usage

1. Integration

Min API level is: API 21

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

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

Step 2. Add the dependency

dependencies {
    ...
    implementation 'com.github.mecoFarid:DynamicSpinner:1.0.1'
}

2. Code Sample

Step 1. Create xml file to hold your single spinner (let's call it item_spinner.xml)

<?xml version="1.0" encoding="utf-8"?>
<com.mecofarid.dynamicspinner.view.SearchableView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/searchable_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:ds_icon_closeSearchView="@drawable/default_close_searchview_icon"
    app:ds_icon_openSearchView="@drawable/default_open_searchview_icon"
    app:ds_iconColor_openSearchView="@color/default_color_open_searchview_icon"
    app:ds_iconColor_closeSearcView="@color/default_color_open_searchview_icon"
    app:ds_isSearchable="true"
    app:ds_textSelectionMode="end"
    app:cardElevation="@dimen/cardview_default_elevation"
    app:cardCornerRadius="@dimen/cardview_default_radius"
    app:cardUseCompatPadding="true"/>

Step 2. Add your DynamicSpinner view to xml file where you want it

<com.mecofarid.dynamicspinner.view.DynamicSpinner
        android:id="@+id/dynamic_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Step 3. Create model objects

Remember to extend ItemSpinner and add @SubCategory annotation to subcategory

class Country: ItemSpinner {

    var name: String? = null
    var code: Integer = 0
    
    @SubCategory
    var cityList:List<City>? = emptyList()
}

Step 4. Add your nested list

class MainActivity : AppCompatActivity(), DynamicSpinnerAdapter.SpinnerItemSelectedListener{

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

         // Nested country list (JSON)
        val planetStructureJson = "{\n" +
                "            \"countryList\": [\n" +
                "            {\n" +
                "                \"code\": 6,\n" +
                "                \"name\": \"Estonia\",\n" +
                "                \"cityList\": [\n" +
                "                {\n" +
                "                    \"name\": \"Tallin\",\n" +
                "                    \"code\": 60\n" +
                "                }\n" +
                "                ]\n" +
                "            }\n" +
                "            ]\n" +
                "        }" 
                
        // You can use Google's Gson library to convert JSON to List 
        val planetStructureList = ...  

        // Initialize adapter
        list?.let {
            dynamic_spinner.adapter = DynamicSpinnerAdapter(it, this, R.layout.item_spinner)
        }
    }

    // You can get selected item in this callback
    override fun onItemSelected(itemSpinner: ItemSpinner) {

    }
}

Attributes

Atribute name Default Available Description
app:ds_icon_closeSearchView any drawable -
app:ds_icon_openSearchView any drawable -
app:ds_iconColor_openSearchView #808080 any color -
app:ds_iconColor_closeSearcView #808080 any color -
app:ds_isSearchable true true | false if true content is searchable
app:ds_textSelectionMode end start | all | end position of cursor when SearchableView opened, all means all text will be selected
app:cardCornerRadius 2dp any dimension radius on the edges
app:cardElevation 2dp any dimension layout elevation
app:cardUseCompatPadding true true | false padding between spinner items

dynamicspinner's People

Contributors

mecofarid avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

dynamicspinner's Issues

Updating subcategories based on current selection is overhead

Currently, all categories update all their subcategories based on the current selection. This causes multiple unnecessary calls to onBindViewHolder(). Due to multiple calls to the same item view from different parent categories, RecyclervView animation lags while updating the views.

Test library Utils

A unit test should be added to test ListParserUtils against different cases.
E.g.

  1. Null object behavior
  2. Empty object behavior
  3. When a model is Kotlin data classes without zero-argument
  4. When @SubCategory annotation is present on a non-list field
  5. When model classes don't inherit "ItemSpinner" base model
  6. Whether each element gets a unique ID when nested list parsed to a hierarchic flat list

Test library performance

Library performance should be tested since it uses recursive function and reflection to make an unreferenced copy of objects without subcategory field

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.