Coder Social home page Coder Social logo

dakusuno / android-magicadapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaelxs/android-magicadapter

0.0 1.0 0.0 2.09 MB

This is a super lightweight tool library that can quickly implement recycleview complex adapters.

License: Apache License 2.0

Kotlin 100.00%

android-magicadapter's Introduction

MagicAdapter

[点击查看中文版]

This is a super lightweight tool library that can quickly implement recycleview complex adapters.

API License

show.gif

Feature

  • Based on Kotlin
  • Based on Databinding
  • No longer need to maintain adapter, there is only one MagicAdapter globally
  • No longer need to maintain viewholder
  • Support multiple item view types
  • Provide some basic callbacks
  • Easily expand headers and footers
  • Easily expand recycleview animation and events
  • No reflection, high efficiency
  • Super lightweight, less than 34 KB

Setup

  1. Add jcenter repository to root's build.gradle
allprojects {
    repositories {
        ...
        jcenter()
    }
}
  1. Turn on databinding in app's build.gradle
android {
    ...
    dataBinding {
        enabled = true
    }
}
  1. Add dependencies in app's build.gradle (please use the latest version)
dependencies {
    ...
    implementation 'com.xuyefeng:magicadapter:1.0.3'
}

Usage

  1. Create a RecycleView item layout file image_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="item"
            type="com.blue.helloadapter.ImageItem" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</layout>

Please note that the name of the XXXItem declared here must be item


  1. Create a XXXItem corresponding to the item layout file, such ad ImageItem
class ImageItem(
        val resId: Int
) : BaseItem() {

    override fun getLayout(): Int = R.layout.image_item_layout

    override fun onBinding(holder: ItemViewHolder) {
        (binding as ImageItemLayoutBinding).apply {
            iv.setImageResource(resId)
            iv.setOnClickListener {
                Toast.makeText(iv.context, "click image on ${holder.adapterPosition}", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

Here is a description of the BaseItem:

  • Can get the Adapter of RecycleView binding
  • Can get the ViewHolder corresponding to item
  • Can get the Databinding corresponding to item

  1. Create a TextItem and a ButtonItem in the same way, and then you can add data.
val adapter = MagicAdapter()
adapter.addItem(ImageItem(R.drawable.s1))
adapter.addItem(TextItem())
adapter.addItem(ButtonItem())
recyclerView.adapter = adapter

  1. If you need a callback, you can do it like this
adapter.itemClick = {
    val position = it.adapterPosition
    val item = it.item
    val binding = it.binding
    ...
}
adapter.itemLongClick = {
    ...
}
adapter.viewAttached = {
    ...
}
adapter.viewDetached = {
    ...
}

It's over here, adding a complex adapter to a recycleview can be as simple as that.

License

Apache-2.0

android-magicadapter's People

Contributors

michaelxs avatar

Watchers

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