Skip to content

Kevin-Kip/SimpleAdapter

Repository files navigation

SimpleAdapter

Simplified recyclerview adapter for android.

Download

Table of Contents

  1. Gradle dependency
  2. Usage
  3. Methods
  4. Contribution
  5. Author
  6. Support
  7. License

Gradle-dependency

dependencies {  
 ... 
 implementation "com.revosleap.adapter:SimpleAdapter:$latest_version"
 }  

Usage

Your activity/fragment needs to implement `SimpleCallbacks` , which is the interface for handling the library's callbacks 😄, like:

Kotlin

class MainActivity : AppCompatActivity(), SimpleCallbacks  

Java

class MainActivity extends AppCompatActivity implements SimpleCallbacks  

OR

Kotlin

val callbacks = object: SimpleCallbacks{
	override fun onViewClicked(view: View, item: Any, position: Int) {}  
  
	override fun bindView(view: View, item: Any, position: Int) {}  
  
	override fun onViewLongClicked(it: View?, item: Any, position: Int) {}
}

This will implemet onViewClicked , onViewLongClicked and bindView methods.
bindView handles binding data to the layout. It's called in the viewholder class and onViewClicked handles clicks on the layout parent.

Both methods receive view - the itemView, item - the object class and position - the position of he item in the list.

Initialize the adapter as follows:

Kotlin

val simpleAdapter = SimpleAdapter(R.layout.list_item, callbacks)  

Java

SimpleAdapter simpleAdapter = new SimpleAdapter(R.layout.list_item, callbacks)  

Attach the adapter to the recyclerview like below and you're good to go.

Kotlin

 list.apply {    
	   adapter = simpleAdapter    
	  layoutManager = LinearLayoutManager(this@MainActivity)    
	  //of course you need a layout manager  
}  

Java

 list.setAdapter(simpleAdapter);    
 list.setLlayoutManager(new LinearLayoutManager(this@MainActivity)); 

Samples

Kotlin

override fun onViewClicked(view: View, item: Any, position: Int) {    
    item as Person    
    Toast.makeText(applicationContext, "Item ${item.name} at position $position clicked", Toast.LENGTH_SHORT).show()    
	simpleAdapter!!.removeItem(item) // removes the clicked item from list
}  
override fun onViewLongClicked(it: View?, item: Any, position: Int) {  
    item as Person  
    Toast.makeText(applicationContext, "Item ${item.name} at position $position long clicked", Toast.LENGTH_SHORT).show()  
}
    val person = Person(R.drawable.preview, "One") 
    simpleAdapter!!.addItem(person)  
override fun bindView(view: View, item: Any, position: Int) {    
    item as Person // smart cast to access its attributes    
  val name = view.textView //textView is the ID of the View in the list item layout    
  val image = view.image //image is also an ID in the layout    
    
 //bind data to the views  name.text = item.name    
image.setImageResource(item.image) //both using modelName and item work }  
private SimpleCallbacks simpleCallbacks = new SimpleCallbacks() {
        @Override
        public void onViewClicked(@NonNull View view, @NonNull Object o, int i) {
        }

        @Override
        public void bindView(@NonNull View view, @NonNull Object o, int i) {
            ShoppingItem item = (ShoppingItem) o;

            SquareImageView imageView = view.findViewById(R.id.cart_item_image);
            TextView itemName = view.findViewById(R.id.cart_item_name);
            TextView itemPrice = view.findViewById(R.id.cart_item_price);
            ImageButton itemMinus = view.findViewById(R.id.cart_item_minus);
            EditText itemCount = view.findViewById(R.id.cart_item_count);
            ImageButton itemPlus = view.findViewById(R.id.cart_item_plus);
            ImageButton itemRemove = view.findViewById(R.id.cart_item_remove);

            Picasso.get()
                    .load(item.getItemImage())
                    .resize(70, 70)
                    .placeholder(R.drawable.placeholder)
                    .error(R.drawable.error)
                    .into(imageView);
            itemName.setText(item.getItemName());
            float price = item.getSelectedCount() * item.getItemPrice();
            itemPrice.setText(String.valueOf(price));
            itemCount.setText(item.getSelectedCount(), TextView.BufferType.EDITABLE);

            itemMinus.setOnClickListener(buttonClicked(Commons.MINUS, item));
            itemPlus.setOnClickListener(buttonClicked(Commons.PLUS, item));
            itemRemove.setOnClickListener(buttonClicked(Commons.REMOVE, item));
        }

        @Override
        public void onViewLongClicked(View view, @NonNull Object o, int i) {
        }
    };

Methods

All the methods provided by the library are:

  1. addManyItems(list: MutableList<Any>) - appends the received list to the existing one.
  2. changeItems(list: MutableList<Any>) - replaces the existing list with one received.
  3. removeItem(position: Int) - removes item at the specified position.
  4. removeItem(item: Any) - removes the received item from the list.
  5. clearItems() - removes all items in the list making it blank. 😉
  6. addItem(position: Int, item: Any) - inserts the received item into the specified position within the list.
  7. addItem(item: Any) - appends the received item to the end of the list.
  8. getPositionOf(item: Any) - find the index of an object.

Contribution

  1. Fork
  2. Create feature branch: git checkout -b my-feature-branch
  3. Commit changes: git commit -am 'new awesome feature'
  4. Push to the branch: git push origin my-feature-branch
  5. Submit a pull request

Author

Made with ❤️ by Kevin Kiprotich

Support

Buy Me A Coffee

License

MIT License

Copyright (c) 2018 Kevin Kiprotich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.