Coder Social home page Coder Social logo

wshtaits / vsbottomnavigationview Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 473 KB

A library for building any BottomNavigationView you want without creating your custom implementation.

Kotlin 100.00%
bottomnavigationview bottom-navigation bottombar bottomnavigation bottomnavigationbar bottom-navigation-view android kotlin-android

vsbottomnavigationview's Introduction

VsBottomNavigationView

A library for building any BottomNavigationView you want without creating your custom implementation.

Gradle

androidExtensions {
    experimental = true //see https://kotlinlang.org/docs/tutorials/android-plugin.html#using-kotlin-android-extensions
}

dependencies {
    implementation 'com.wshtaits:vsbottomnavigationview:1.0.0'
}

Methods

fun setAutoSelectable(isAutoSelectable: Boolean)

fun setItemAnimator(animator: ItemAnimator?)

fun setItemAnimatorForPosition(itemPosition: Int, animator: ItemAnimator)

fun setItemAnimatorForId(@IdRes itemId: Int, animator: ItemAnimator)

fun setOnItemClickAction(action: ((itemId: Int) -> Unit)?)

fun setOnItemSelectAction(action: ((itemId: Int) -> Unit)?)

fun selectItemById(@IdRes itemId: Int)

fun selectItemByPosition(itemPosition: Int)

Simple example

layout/activity_sample.xml

<com.wshtaits.vsbottomnavigationview.VsBottomNavigationView
    android:id="@+id/vs_bottom_navigation_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:elevation="8dp"
    app:menu="@menu/sample"
    app:itemLayout="@layout/item_sample" />
    

layout/item_sample.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="?selectableItemBackgroundBorderless"
    >

    <ImageView
        android:id="@+id/icon_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        />

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        />

</LinearLayout>

menu/sample.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/action_wifi"
        android:title="@string/wifi"
        android:icon="@drawable/ic_wifi"
        android:checked="true"
        />

    <item
        android:id="@+id/action_cellular"
        android:title="@string/cellular"
        android:icon="@drawable/ic_cellular"
        />

    <item
        android:id="@+id/action_battery"
        android:title="@string/battery"
        android:icon="@drawable/ic_battery"
        />

</menu>

SampleItemAnimator.kt

class SampleItemAnimator(context: Context) : ItemAnimator() {

    private val selectedColor = context.getColor(android.R.color.black)
    private val unselectedColor = context.getColor(android.R.color.darker_gray)

    override fun ItemViewHolder.onCreate(menuItem: MenuItem) {
        icon_iv.background = menuItem.icon
        title_tv.text = menuItem.title

        if (menuItem.isChecked) {
            menuItem.icon.setTint(selectedColor)
            title_tv.setTextColor(selectedColor)
        } else {
            menuItem.icon.setTint(unselectedColor)
            title_tv.setTextColor(unselectedColor)
        }
    }

    override fun ItemViewHolder.onSelect(menuItem: MenuItem) {
        menuItem.icon.setTint(selectedColor)
        title_tv.setTextColor(selectedColor)
    }

    override fun ItemViewHolder.onDeselect(menuItem: MenuItem) {
        menuItem.icon.setTint(unselectedColor)
        title_tv.setTextColor(unselectedColor)
    }
}

SampleActivity.kt

vs_bottom_navigation_view.setItemAnimator(SampleItemAnimator())

Animated example

Just change ItemAnimator implementation to something like this: SampleItemAnimator.kt

Specific item example

Create specific item layout layout/item_sample_specific.xml and separate ItemAnimator for it SpecificSampleItemAnimator.kt.

Then add it inside VsBottomNavigationView tag.

<com.wshtaits.vsbottomnavigationview.VsBottomNavigationView
    android:id="@+id/vs_bottom_navigation_view"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    android:background="#fff"
    android:elevation="8dp"
    app:menu="@menu/sample"
    app:itemLayout="@layout/item_sample"
    tools:targetApi="lollipop" >

    <include layout="@layout/item_sample_specific" />

</com.wshtaits.vsbottomnavigationview.VsBottomNavigationView>

And in Activity:

vs_bottom_navigation_view.setItemAnimatorForId(R.id.action_battery, SpecificSampleItemAnimator(this))

Note:

  • layout_menuPosition corresponds to a menu item at a specified position.
  • Root view id of your specific item layout become menu item id.
  • If you use <include> tag then layout_menuPosition attribute must be specified where you specified other layout_* attributes.

isAutoSelectable

If you want to handle the event first, and only then decide whether to make it selected then set isAutoSelectable attribute to false through xml or code and use setOnItemClickAction and selectItemBy* methods.

License

Copyright (c) 2019 Shtaits Valeriy.

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.

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.