All Projects → vpaliy → MultiChoiceMode-RecyclerView

vpaliy / MultiChoiceMode-RecyclerView

Licence: other
This repository provides an API for creating a multi choice mode in RecyclerView.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to MultiChoiceMode-RecyclerView

Pursuit-Core-Android
Pursuit Core Android
Stars: ✭ 45 (+125%)
Mutual labels:  recyclerview
slush
This library will no longer be updated 😭
Stars: ✭ 26 (+30%)
Mutual labels:  recyclerview
ExpandableRecyclerView
ExpandableRecyclerView with smoothly animation.
Stars: ✭ 412 (+1960%)
Mutual labels:  recyclerview
Recycling
A Library for make an easy and faster RecyclerView without adapter
Stars: ✭ 57 (+185%)
Mutual labels:  recyclerview
My Android Garage
A quick reference guide for Android development.
Stars: ✭ 66 (+230%)
Mutual labels:  recyclerview
SpacingItemDecoration
ItemDecoration for RecyclerView that allows you to set spacing between and around list items in flexible way.
Stars: ✭ 83 (+315%)
Mutual labels:  recyclerview
RecyclerViewExtensionsDemo
RecyclerView列表优化方案
Stars: ✭ 45 (+125%)
Mutual labels:  recyclerview
Android
Step by step guide for various components in android
Stars: ✭ 32 (+60%)
Mutual labels:  recyclerview
KRecyclerDsl
Kotlin Dsl for Android RecyclerView
Stars: ✭ 14 (-30%)
Mutual labels:  recyclerview
TheGreatAdapter
Multiple items adapter made too easy, including headers and footers.
Stars: ✭ 46 (+130%)
Mutual labels:  recyclerview
Android-HeaderAndFooterRecyclerView
Let RecyclerView support add HeaderView and FooterView.
Stars: ✭ 36 (+80%)
Mutual labels:  recyclerview
PrimeAdapter
PrimeAdapter makes working with RecyclerView easier.
Stars: ✭ 54 (+170%)
Mutual labels:  recyclerview
MixAdapter
Compose multiple Adapter for RecyclerView in Android
Stars: ✭ 19 (-5%)
Mutual labels:  recyclerview
ZigzagRecyclerView
A library that gives you a slant on the traditional Grid Recycler View ♻️
Stars: ✭ 27 (+35%)
Mutual labels:  recyclerview
JADA
JADA - Just Another Dictionary App
Stars: ✭ 20 (+0%)
Mutual labels:  recyclerview
ParkingDemo
Taipei City Parking Lot Information Query System Demo
Stars: ✭ 18 (-10%)
Mutual labels:  recyclerview
ARVI
Android library designed to simplify the implementation of the video autoplay in the RecyclerView
Stars: ✭ 69 (+245%)
Mutual labels:  recyclerview
Adsorbent
Adsorbent of RecyclerView , RecyclerView吸顶
Stars: ✭ 25 (+25%)
Mutual labels:  recyclerview
Simple-Notes-Kotlin-App
✍️ Simple Note Making App use mvvm architecture , dagger , coroutines and navigation component. Features includes 🗒️ create , edit and ❌ delete notes
Stars: ✭ 40 (+100%)
Mutual labels:  recyclerview
Android-Code-Demos
📦 Android learning code demos.
Stars: ✭ 41 (+105%)
Mutual labels:  recyclerview

MultiChoiceMode-RecyclerView

API
This repository provides an API for creating a multiple choice mode in RecyclerView.

A brief glance at the sample

Get on Google Play

How does it work?

The API contains three main classes:BaseAdapter, MultiMode and StateTracker. The last one has a package-private access, so you don't need to deal with it at all.

Basically, you need to do the following steps in order to make it work:

  • Get a toolbar and create a MultiMode instance using the builder approach:

      /** You can set colors, menu to inflate and callback to use, navigation icon, logo, title and subtitle */
      MultiMode mode=new MultiMode.Builder(actionBar,this)
                .setMenu(R.menu.list_menu,new MultiMode.Callback() {
                    @Override
                    public boolean onMenuItemClick(BaseAdapter adapter, MenuItem item) {
                        // you can use any function of the adapter in order to work with selected items
                        return false;
                    }
                })
                .setStatusBarColor(Color.MAGENTA)
                .setBackgroundColor(Color.WHITE)
                .setNavigationIcon(getResources().getDrawable(R.drawable.ic_clear_black_24dp))
                .build();
  • Extend the BaseAdapter class and initialize it using the MultiMode instance:

     public class Adapter extends BaseAdapter {
     
         public Adapter(MultiMode mode, boolean animate){
            super(mode,animate);
         }
         
         public Adapter(MultiMode mode, boolean animate, @NonNull Bundle savedInstanceState){
            super(mode,animate,savedInstanceState);
         }
         
         //implement the rest of the important methods which RecyclerView has
         
         @Override
         public void removeAt(int index){
            //write your implementation of this method
         }
     }
  • Then create a ViewHolder class of your adapter using a special class in BaseAdapter and implement the next methods:

    public class ViewHolder extends BaseAdapter.BaseViewHolder {
    
        public ViewHolder(View itemView) {
            super(itemView);
            //bind your views here
        }
    
        @Override
        public void onBindData() {
            //install your views with the data here 
            
            //after you have installed your data, call this method, otherwise you will not get any animation
            determineState();
        }
    
        @Override
        public void updateBackground() {
            if(isChecked(getAdapterPosition())){
                //if this item has been selected, set an appropriate background
            }else{
               //install the background of your view
            }
        }
    
        @Override
        public void enterState() {
            super.enterState();
            //animate your view 
        }
    
        @Override
        public void animatedState() {
            //set your view to the animated state, without animation 
        }
    
        @Override
        public void exitState() {
            super.exitState();
            //create your exit animation
        }
    
        @Override
        public void defaultState() {
           //set your view to the default state
        }
     }

Understanding of animation in the BaseAdapter.BaseViewHolder class

If you decide to animate an item upon click, you have to understand how it works. Basically, there are 4 states of the item:

  1. Enter state
    This state represents an animation that has to occur when an item is clicked. For example, you can use this implementation of the method:
     @Override
     public void enterState() {
        super.enterState();
        itemView.animate()
          .scaleX(0.85f)
          .scaleY(0.85f)
          .setDuration(180).start();
     }
  1. Animated state
    This is a state of an item that has been animated, and doesn't need this animation again. So, the main purpose of this method is to put a view into the animated state.
     @Override
     public void animatedState() {
       itemView.setScale(0.85f);
       itemView.setScale(0.85f);
     }
  1. Exit state
    This state is responsible for animating a view in the normal state, some kind of back animation. Here is the example:
     @Override
     public void exitState() {
       super.exitState();
       itemView.animate()
         .scaleX(1.f)
         .scaleY(1.f)
         .setDuration(180).start();
     }
  1. Default state
    That is a default or normal state of your item. The following method can be a representation of such state:
      @Override
      public void defaultState() {
         if(itemView.getScale()<1.f){
            itemView.setScale(1.f);
            itemView.setScale(1.f);
          }
      }

Once again, if you don't want to animate items, you are not obliged to implement those methods.

What if screen rotation occurs?

As long as you have a reference to your adapter, screen rotation is not a big deal. You can easily save and restore the state of the adapter using the following code:

  • Save the state:

     @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         adapter.saveState(outState);
     }    
  • Restore:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //do some stuff$
        
        if(savedInstanceState!=null){
          adapter=new Adapter(mode,true,savedInstanceState);
        }else{
          adapter=new Adapter(mode,true);
        }
    }

Also you need to keep in mind that activity can be stopped by launching another app or receiving a phone call. In this particular case that is not a problem as well, as long as you keep the reference to your adapter, you can restore it:

 @Override
 protected void onResume() {
     super.onResume();
     if(adapter!=null){
         adapter.onResume();
     }
 }

License

MIT License

Copyright (c) 2016 Vasyl Paliy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].