项目作者: vpaliy

项目描述 :
This repository provides an API for creating a multi choice mode in RecyclerView.
高级语言: Java
项目地址: git://github.com/vpaliy/MultiChoiceMode-RecyclerView.git
创建时间: 2017-01-21T06:09:48Z
项目社区:https://github.com/vpaliy/MultiChoiceMode-RecyclerView

开源协议:

下载


MultiChoiceMode-RecyclerView

API

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

A brief glance at the sample

.gif)

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:

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

    1. public class Adapter extends BaseAdapter {
    2. public Adapter(MultiMode mode, boolean animate){
    3. super(mode,animate);
    4. }
    5. public Adapter(MultiMode mode, boolean animate, @NonNull Bundle savedInstanceState){
    6. super(mode,animate,savedInstanceState);
    7. }
    8. //implement the rest of the important methods which RecyclerView has
    9. @Override
    10. public void removeAt(int index){
    11. //write your implementation of this method
    12. }
    13. }
  • Then create a ViewHolder class of your adapter using a special class in BaseAdapter and implement the next methods:

    1. public class ViewHolder extends BaseAdapter.BaseViewHolder {
    2. public ViewHolder(View itemView) {
    3. super(itemView);
    4. //bind your views here
    5. }
    6. @Override
    7. public void onBindData() {
    8. //install your views with the data here
    9. //after you have installed your data, call this method, otherwise you will not get any animation
    10. determineState();
    11. }
    12. @Override
    13. public void updateBackground() {
    14. if(isChecked(getAdapterPosition())){
    15. //if this item has been selected, set an appropriate background
    16. }else{
    17. //install the background of your view
    18. }
    19. }
    20. @Override
    21. public void enterState() {
    22. super.enterState();
    23. //animate your view
    24. }
    25. @Override
    26. public void animatedState() {
    27. //set your view to the animated state, without animation
    28. }
    29. @Override
    30. public void exitState() {
    31. super.exitState();
    32. //create your exit animation
    33. }
    34. @Override
    35. public void defaultState() {
    36. //set your view to the default state
    37. }
    38. }


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:

  1. @Override
  2. public void enterState() {
  3. super.enterState();
  4. itemView.animate()
  5. .scaleX(0.85f)
  6. .scaleY(0.85f)
  7. .setDuration(180).start();
  8. }

2) 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.

  1. @Override
  2. public void animatedState() {
  3. itemView.setScale(0.85f);
  4. itemView.setScale(0.85f);
  5. }

3) Exit state

This state is responsible for animating a view in the normal state, some kind of back animation. Here is the example:

  1. @Override
  2. public void exitState() {
  3. super.exitState();
  4. itemView.animate()
  5. .scaleX(1.f)
  6. .scaleY(1.f)
  7. .setDuration(180).start();
  8. }

4) Default state

That is a default or normal state of your item. The following method can be a representation of such state:

  1. @Override
  2. public void defaultState() {
  3. if(itemView.getScale()<1.f){
  4. itemView.setScale(1.f);
  5. itemView.setScale(1.f);
  6. }
  7. }

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:

    1. @Override
    2. protected void onSaveInstanceState(Bundle outState) {
    3. super.onSaveInstanceState(outState);
    4. adapter.saveState(outState);
    5. }
  • Restore:

    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. //do some stuff$
    5. if(savedInstanceState!=null){
    6. adapter=new Adapter(mode,true,savedInstanceState);
    7. }else{
    8. adapter=new Adapter(mode,true);
    9. }
    10. }

    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:

    1. @Override
    2. protected void onResume() {
    3. super.onResume();
    4. if(adapter!=null){
    5. adapter.onResume();
    6. }
    7. }

License

  1. MIT License
  2. Copyright (c) 2016 Vasyl Paliy
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.