ChipRecyclerView

Additional

Language
Kotlin
Version
Release-1.0.5 (Sep 20, 2021)
Created
Sep 14, 2021
Updated
Jan 28, 2022 (Retired)
Owner
vedraj360
Contributors
vedraj360
InnovativesApp
2
Activity
Badge
Generate
Download
Source code

ChipRecyclerView 🔥

A simple library to Create Circular chips like Instagram stories or a custom recyclerView without need of adapter..

🎬Preview

CircularChipViewHolder & CircularTextChipViewHolder

How it works:

  1. Gradle Dependency
  • Add the JitPack repository to your project's build.gradle file
    allprojects {
        repositories {
         maven { url 'https://jitpack.io' }
        }
    }
  • Add the dependency
dependencies {
         implementation 'com.github.vedraj360:ChipRecyclerView:Release-1.0.5'
 }

Adding to the layout

Add ChipRV to your layout.

   <com.vdx.chiprv.ChipRV
        android:id="@+id/RecyclerView2"
        android:layout_width="match_parent"
        android:layout_height="100dp" />
  • There are multiple viewholders already present in library out of the box .

    1 CircularChipViewHolder

    2 CircularTextChipViewHolder

Now, in your Activity or Fragment, you can simply initialize your ChipRV.

  val recyclerView: ChipRV<ChipItem> = findViewById(R.id.RecyclerView)
        recyclerView.init(this)
            .listener(object : ChipClickListener<ChipItem> {
                override fun onClick(item: ChipItem, position: Int, clickEventCode: Int) {
                    if (item is ChipData) {
                        Toast.makeText(
                            this@MainActivity,
                            item.name,
                            Toast.LENGTH_SHORT
                        ).show();
                    }
                }
            })
            .setSelection(true)
            .layout().linear().orientation(RVOrientation.HORIZONTAL)
            .addView().item(ChipData::class.java).holder(CircularChipViewHolder::class.java)
            .layout(R.layout.chip_li)
            .build()

        recyclerView.addAll(chipLists()) // List of items.

If you don't want to use the given view holders you can create your own custom view holder and data class and pass them to recyclerView.

See Documentation for more details. 💻

Documentation

Creating custom view holder

Create a class which extends from ChipViewHolder depending on your item.

Don't forget to create ChipItem before creating ChipViewHolder.

  • To create ChipItem just extends the model class with ChipItem.
data class ChipData(val name: String, val url: String) : ChipItem()
  • Custom viewholder.
class CircularChipViewHolder(
    private val itemView: View,
    clickEventListener: ChipViewHolderClickEventListener
) : ChipViewHolder<ChipData>(itemView, clickEventListener) {
    private lateinit var nameTv: TextView
    private lateinit var circleImageView: CircleImageView
    private lateinit var dot: ImageView

    override fun bind(item: ChipData, position: Int) {
        nameTv.text = item.name
        Glide.with(itemView.context).load(item.url)
            .into(circleImageView)
        if (item.selected) {
            dot.visibility = View.VISIBLE
            circleImageView.borderWidth = 8
            circleImageView.borderColor = ContextCompat.getColor(circleImageView.context, R.color.red)
        } else {
            dot.visibility = View.GONE
            circleImageView.borderWidth = 2
            circleImageView.borderColor = ContextCompat.getColor(circleImageView.context, R.color.black)
        }
    }

    override fun init() {
        nameTv = init(R.id.name) as TextView
        circleImageView = init(R.id.circleImageView) as CircleImageView
        dot = init(R.id.dot) as ImageView
    }

    override fun assign() {
        assignClickEvent(circleImageView)
    }
}
public class QualityViewHolder extends ChipViewHolder<QualityModel> {
    private ImageView image;
    private ImageView videoIcon;
    private TextView date;

    private final View itemView;


    public QualityViewHolder(@NonNull View itemView, @NonNull ChipViewHolderClickEventListener clickEventListener) {
        super(itemView, clickEventListener);
        this.itemView = itemView;
    }

    @Override
    public void init() {
        image = (ImageView) init(R.id.quality_image);
        videoIcon = (ImageView) init(R.id.video_icon);
        date = (TextView) init(R.id.quality_date);
    }

    @Override
    public void assign() {
        assignClickEvent(image);
        assignLongClickEvent(image, 101);
    }

    @Override
    public void bind(@NonNull QualityModel qualityModel, int position) {
        if (qualityModel.getCreationDate() != null) {
            date.setVisibility(View.VISIBLE);
            date.setText(Helper.formatDateByPattern(qualityModel.getCreationDate(),"yyyy-MM-dd","dd-MMM"));
        }
        if (qualityModel.getType() != null) {
            if (qualityModel.getType().equals("IMAGE")) {
                Glide.with(itemView.getContext()).load(qualityModel.getMediaUrl()).into(image);
                videoIcon.setVisibility(View.GONE);
            } else {
                videoIcon.setVisibility(View.VISIBLE);
                if (qualityModel.getThumbnail() != null) {
                    Glide.with(itemView.getContext()).load(qualityModel.getThumbnail()).into(image);
                } else {
                    Glide.with(itemView.getContext()).load(R.drawable.ic_add_white).into(image);
                }
            }
        }
    }
}

Constructuring

init(Context context)

returns ChipRV's itself.

listener(ChipClickListener recyclerClickListener)

returns ChipRV's itself.

layout()

Use layout().linear()and see the options below. After all calladdView()` to begin adding view holders.

layout().orientation()
method params default returns description
orientation() RVOrientation VERTICAL itself Sets the orientation of grid layout. RVOrientation.VERTICAL, RVOrientation.HORIZONTAL
reverse() boolean false itself Sets the reverse ordering.
span() int 1 itself Sets the total span count of one row.
addView() - - ChipViewHolderBuilder Starts to add view holders.

Example

layout()
    .orientation(RVOrientation.VERTICAL) // optional
    .reverse(false) // optional
    .addView() // end
    
layout().linear()
method params default returns description
orientation() RVOrientation VERTICAL itself Sets the orientation of grid layout. RVOrientation.VERTICAL, RVOrientation.HORIZONTAL
reverse() boolean false itself Sets the reverse ordering.
addView() - - ChipViewHolder Starts to add view holders.

Example

layout()
    .linear() // start
    .orientation(RVOrientation.VERTICAL) // optional
    .reverse(false) // optional
    .addView() // end
    
setSelection()

If setSelection is set true you can make view act as the single selection of item. In viewholder's data item you get a selected boolean from you can identiy which item is selected.

    // ChipData is is model class 
    override fun bind(item: ChipData, position: Int) {
        nameTv.text = item.name
        Glide.with(itemView.context).load(item.url)
            .into(circleImageView)
        if (item.selected) {
            dot.visibility = View.VISIBLE
            circleImageView.borderWidth = 8
            circleImageView.borderColor = ContextCompat.getColor(circleImageView.context, R.color.red)
        } else {
            dot.visibility = View.GONE
            circleImageView.borderWidth = 2
            circleImageView.borderColor = ContextCompat.getColor(circleImageView.context, R.color.black)
        }
    }
addView()
method params default returns description
holder() Class REQUIRED itself Sets the class of holder which you created before.
item() Class REQUIRED itself Sets the class of item associated with your holder.
layout() int REQUIRED itself Sets the layout id of your view holder.
addView() - - ChipViewHolderBuilder Starts to add more view holders.
build() - - GRView Ends the build process.

Example

addView() // start
.item(ChipTextData::class.java) // item class
.holder(CircularTextChipViewHolder::class.java) // holder class
.layout(R.layout.chip_li) // holder resource
.build() // finally build

Items

method params description
set() ArrayList<T> Sets the items
addAll() ArrayList<T> Adds all items
add() T Adds the item
remove() - Clears all items
remove() int Removes the item at given index
remove() T Removes the item
update() T Updates the given item
getItems() ArrayList<T> Returns all items

Other Library used:

Find this library useful? ❤️

Support it by joining stargazers for this repository.

🤝 How to Contribute

Whether you're helping us fix bugs, improve the docs, or a feature request, we'd love to have you! 💪

Check out our Contributing Guide for ideas on contributing.

Bugs and Feedback

For bugs, feature requests, and discussion please use GitHub Issues.

License

MIT License

Copyright (c) 2021 vedraj360

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.