Coder Social home page Coder Social logo

recycleradapters's Introduction

RecyclerAdapters

Introduction

RecyclerAdapters provides you a very simple way for implement RecyclerView.Adapter class in your projects. With this library you can implement Adapter in a few line.

Get started

dependencies {
    compile 'com.github.alex-michenko:recycleradapters:0.2.4'
}

How to use

1.Create DataHolder

Create class-wrapper that will hold data for adapter:

public class PersonDH extends RecyclerDH {

  public boolean isFavourite;
  public int countStars;
  public String name;

  public String headerName;

  public PersonDH(String name, boolean isFavourite) {
      this.name = name;
      this.isFavourite = isFavourite;

      if (isFavourite)
          countStars = new Random().nextInt(4) + 1;
  }

  public PersonDH(String headerName) {
      this.headerName = headerName;
  }
}

2. Create ViewHolder

Create ViewHolder class and set generic parameter as created DataHolder:

public class PersonVH extends RecyclerVH<PersonDH> {

  private TextView tvNamePerson;

  public PersonVH(View itemView, @Nullable final OnCardClickListener listener, final int viewType) {
      super(itemView, listener, viewType);

      tvNamePerson = findView(R.id.tvNamePerson);

      tvNamePerson.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (listener != null) {
                  listener.onClick(view, getAdapterPosition(), viewType);
              }
          }
      });
  }

  @Override
  public void bindData(PersonDH data) {
      tvNamePerson.setText(data.name);
  }
}

If you adapter has several viewTypes then create suitable ViewHolders:

public class FavouriteVH extends RecyclerVH<PersonDH> {
  //.....
}
public class HeaderVH extends RecyclerVH<PersonDH> {
  //.....
}

3. Create adapter

If all items have same viewType then you need to extend SimpleRecyclerAdapter :

public class ContactAdapter extends SimpleRecyclerAdapter<PersonDH, PersonVH> {

  @Override
  protected int getItemLayout() {
      return R.layout.item_person;
  }
}

You need to override method getItemLayout() and define layout resourse for item.

For several viewTypes you need to extend TypedRecyclerAdapter :

public class PersonAdapter extends TypedRecyclerAdapter<PersonDH> {

  private static final int HEADER = 1;
  private static final int PERSON = 11;
  private static final int FAVOURITE = 0;

  @Override
  protected void initViewTypes() {
      addType(PERSON, R.layout.item_person, PersonVH.class);
      addType(HEADER, R.layout.item_header, HeaderVH.class);
      addType(FAVOURITE, R.layout.item_favourite, FavouriteVH.class);
  }

  @Override
  protected int getViewType(int position) {
      PersonDH dh = getItem(position);
      if (dh.headerName != null) {
          return HEADER;
      } else {
          return dh.isFavourite ? FAVOURITE : PERSON;
      }
  }
}

In initViewTypes() you need to define all viewTypes of adapter using method addType().

getViewType() is similar RecyclerView.Adapter#getItemViewType()

4. Use adapter

Init your adapter and set in RecyclerView :

      rvSimpleList = (RecyclerView) view.findViewById(R.id.rvTypedList);
      rvSimpleList.setLayoutManager(new LinearLayoutManager(getContext()));

      adapter = new PersonAdapter();
      adapter.setOnCardClickListener(new OnCardClickListener() {
          @Override
          public void onClick(View view, int position, int viewType) {
              //do something
          }
      });

      rvSimpleList.setAdapter(adapter);

The default OnCardClickListener#onClick() is called when clicked on item. You may define in ViewHolder other calls of method.

For more information see sample.

License

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.

recycleradapters's People

Contributors

alex-michenko avatar

Watchers

James Cloos avatar Nikhil 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.