Simplified recyclerview adapter for android.
dependencies {
...
implementation "com.revosleap.adapter:SimpleAdapter:$latest_version"
}
Kotlin
class MainActivity : AppCompatActivity(), SimpleCallbacks
Java
class MainActivity extends AppCompatActivity implements SimpleCallbacks
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 andposition
- 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));
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) {
}
};
All the methods provided by the library are:
addManyItems(list: MutableList<Any>)
- appends the received list to the existing one.changeItems(list: MutableList<Any>)
- replaces the existing list with one received.removeItem(position: Int)
- removes item at the specified position.removeItem(item: Any)
- removes the received item from the list.clearItems()
- removes all items in the list making it blank. 😉addItem(position: Int, item: Any)
- inserts the received item into the specified position within the list.addItem(item: Any)
- appends the received item to the end of the list.getPositionOf(item: Any)
- find the index of an object.
- Fork
- Create feature branch:
git checkout -b my-feature-branch
- Commit changes:
git commit -am 'new awesome feature'
- Push to the branch:
git push origin my-feature-branch
- Submit a pull request
Made with ❤️ by Kevin Kiprotich
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.