GithubHelp home page GithubHelp logo

onurciner / flexadapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ajalt/flexadapter

0.0 2.0 0.0 18.88 MB

The easiest way to use a RecyclerView on Android

License: Apache License 2.0

Kotlin 79.54% Java 20.46%

flexadapter's Introduction

Define and coordinate multiple layouts in a RecyclerView or ViewPager without boilerplate.

  • Multiple item layouts in a single adapter with no typecasting.
  • Per-item span, swipe, and drag behavior in a RecyclerView in just a few lines of code.
  • Add, remove, and update multiple layouts in a ViewPager without touching Fragments.

Usage

The examples will be in Kotlin, but this library works just as well with Java.

Create the adapter:

// Create the adapter
val adapter = FlexAdapter()
recyclerView.adapter = adapter
val layoutManager = GridLayoutManager(this, 3)
// Add this line to enable per-item spans on GridLayoutManagers
layoutManager.spanSizeLookup = adapter.spanSizeLookup
recyclerView.layoutManager = layoutManager

Define your item types:

// This item will be a text header with a span of three that can be swiped horizontally to dismiss.
class TextItem(var text: String) :
        FlexAdapterExtensionItem(R.layout.item_text, span = 3, swipeDirs = HORIZONTAL) {
    override fun bindItemView(itemView: View, position: Int) {
        itemView.text_view.text = text
    }
}

// This will be a picture loaded from a resource the can be reordered by dragging in any direction.
class PictureItem(@DrawableRes val imageRes: Int) :
        FlexAdapterExtensionItem(R.layout.item_picture, dragDirs = ALL_DIRS) {
    override fun bindItemView(itemView: View, position: Int) {
        itemView.image_view.setImageResource(imageRes)
    }
}

Each layout in the adapter gets its own item class, which has fields for any mutable data in its layout. Since the items own the data and know how to bind it to a view holder, there are no intermediate data holder classes, no interfaces, and no casting to a base class and back.

Then add some items:

adapter.addItems(
    TextItem("Look at these pictures"),
    PictureItem(R.drawable.picture_1),
    PictureItem(R.drawable.picture_2)
)

Update an existing item:

// If we added this TextItem earlier
textItem.text = "Look at this new text"
adapter.notifyItemChanged(textItem)

When you want to update an item, just change its data and notify the adapter that it changed.

That's it. No managing indices. No casting from interfaces or Object. Just fast, simple code that does exactly what you want.

API Documentation

API documentation is hosted online here.

Sample project

To see more of the features of FlexAdapter in use, check out the Kotlin sample app here, or the Java sample app here

FlexAdapter for a ViewPager

This library also includes an adapter for a ViewPager that provides the same interface as the regular FlexAdapter: the FlexPagerAdapter

val adapter = FlexPagerAdapter()

adapter.addItems(
    TextItem("Look at these pictures"),
    PictureItem(R.drawable.picture_1),
    PictureItem(R.drawable.picture_2)
)

view_pager.adapter = adapter

That's all that's required to implement multiple layouts in a ViewPager. No need to create Fragments or manage lifecycles. The same classes can be used in both the FlexAdapter and FlexPagerAdapter. The span, dragDirs, and swipeDirs values will just be ignored on item added to a FlexPagerAdapter.

You can see a sample that uses the FlexPagerAdapter here.

Download

FlexAdapter is distributed with JitPack

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
   compile 'com.github.ajalt:flexadapter:1.6.1'
}

License

Copyright 2016 AJ Alt

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.

flexadapter's People

Contributors

ajalt avatar xiphirx avatar

Watchers

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