Git Product home page Git Product logo

autodroid's Introduction

AutoDroid

AutoDroid automates some common Android development tasks

1. RecyclerView

RecylerView is awesome, but it require you to create several boilerplate code just to make it work -- such as

  1. Create an Adapter class
  2. Create a ViewHolder class
  3. Declare and initialize the ViewHolder with all the views
  4. Implement Adapter methods such as
    1. onCreateViewHolder
    2. onBindViewHolder - Bind each of the views in ViewHolder with the data

In Code it looks something like -

    override fun onCreate(savedInstanceState: Bundle?) {
		
		.....
		
        viewManager = LinearLayoutManager(this)
        viewAdapter = MyAdapter(dataSet)
        
        recyclerView = findViewById<RecyclerView>(R.id.recyclerView).apply {
            layoutManager = viewManager

            adapter = viewAdapter
        }

    }
    
 /**
 * Adapter for the recycler view
 */
class MyAdapter(private val myDataset: MutableList<MyData>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private var onClickListener: View.OnClickListener? = null;

    //********************************************************************************
    /**
     * View holder for recycler view
     */
    class ViewHolder(mainView: View, private val onClickListener: View.OnClickListener) : RecyclerView.ViewHolder(mainView) {

        /**
         * Keep hold of the first name text view
         */
        val firstName: TextView = mainView.findViewById(R.id.textView)

        /**
         * Keep hold of the second name text view
         */
        val secondName: TextView = mainView.findViewById(R.id.textView2)

        /**
         * Keep hold of the image view for image resource
         */
        val imageResource: ImageView = mainView.findViewById(R.id.imageView)

        /**
         * Keep hold of the add button to add click listener
         */
        val addBtn: Button = mainView.findViewById(R.id.button1)

        /**
         * Keep hold of the remove button to add click listener
         */
        val removeBtn: Button = mainView.findViewById(R.id.button2)


        init {
            //add the click listeners
            addBtn.setOnClickListener { onClickListener.onClick(addBtn) }
            removeBtn.setOnClickListener { onClickListener.onClick(removeBtn) }
        }
    }
    //********************************************************************************


    fun setOnClickListener(onClickListener: View.OnClickListener) {
        this.onClickListener = onClickListener
    }

    /**
     * Create the view holder
     */
    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): MyAdapter.ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.my_text_view, parent, false),
                View.OnClickListener {
                    onClickListener?.onClick(it)
                })
    }

    /**
     * Bind the view holder with the data
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //bind the first name with the data
        holder.firstName.text = myDataset[position].firstName

        //bind the second name with the data
        holder.secondName.text = myDataset[position].secondName

        //bind the image using picaso
        Picasso.get().load(myDataset[position].imageResource).into(holder.imageResource)
    }

    override fun getItemCount(): Int {
        return myDataset.size
    }
}
    




With AutoDroid, all you have to do is :

//Set the adapter to the AutoDroid auto generated class
recyclerView.adapter = MyDataAdapter(Pager(listDataProvider))

  • No need to write Adapter, AutoDroid creates it for you
  • No need to write ViewHolder, AutoDroid internally takes care of it
  • No need of manually binding each view to data, AutoDroid takes care of it
  • All you have to do is annotate your data class with the layout and view bindings
  • No need of manually notifying adapter when data changes. Adapter generated by AutoDroid, listens for the data added/removed events takes care of it
  • Supports Text and Image data types
  • Uses Picaso to load images as needed
  • Uses Pager to manage data, allowing you to easily add/remove/update data

To use AutoDroid, simply annotate your data class for the recycler as follows

@ItemLayoutRes(layoutID = R.layout.itemView)
data class MyData(@ItemIdRes(viewID = R.id.textView) val firstName: String,
                  @ItemIdRes(viewID = R.id.imageView, itemType = ItemType.IMAGE) val imageResource: Int)

AutoDroid will generate the Adapter for you (In this case MyDataAdapter). Simply set this as the adapter of your recycler view.

See example

That's it!

Getting AutoDroid

Gradle dependency

dependencies {
    implementation 'com.josesamuel:autodroid-api:1.0.1'
    kapt 'com.josesamuel: autodroid:1.0.1'
    implementation 'com.josesamuel:pager:1.0.4'
    
    //Add Picaso if the any data binding uses Image type
    implementation 'com.squareup.picasso:picasso:2.71828'
    
}

License

Copyright 2018 Joseph Samuel

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.

autodroid's People

Contributors

josesamuel avatar

Watchers

 avatar  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.