danoz73 / RecyclerViewFastScroller

A Fast Scroller for the RecyclerView world!
Other
1.13k stars 211 forks source link

Help with usage #74

Open iamchurchill opened 6 years ago

iamchurchill commented 6 years ago

Hi thanks for this wonderful library. please can you help with how i can use it for my recyclerview.

this is my model: public class Country { private int id; private String name, iso2name, iso3name, iso2continent, phone_code, currency, active, added, updated;

public Country() {
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active, String added, String updated) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
    this.added = added;
    this.updated = updated;
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
}

public Country(String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active, String added, String updated) {
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
    this.added = added;
    this.updated = updated;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getIso2name() {
    return iso2name;
}

public void setIso2name(String iso2name) {
    this.iso2name = iso2name;
}

public String getIso3name() {
    return iso3name;
}

public void setIso3name(String iso3name) {
    this.iso3name = iso3name;
}

public String getIso2continent() {
    return iso2continent;
}

public void setIso2continent(String iso2continent) {
    this.iso2continent = iso2continent;
}

public String getPhone_code() {
    return phone_code;
}

public void setPhone_code(String phone_code) {
    this.phone_code = phone_code;
}

public String getCurrency() {
    return currency;
}

public void setCurrency(String currency) {
    this.currency = currency;
}

public String getActive() {
    return active;
}

public void setActive(String active) {
    this.active = active;
}

public String getAdded() {
    return added;
}

public void setAdded(String added) {
    this.added = added;
}

public String getUpdated() {
    return updated;
}

public void setUpdated(String updated) {
    this.updated = updated;
}

@Override
public String toString() {
    return "Country{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", iso2name='" + iso2name + '\'' +
            ", iso3name='" + iso3name + '\'' +
            ", iso2continent='" + iso2continent + '\'' +
            ", phone_code='" + phone_code + '\'' +
            ", currency='" + currency + '\'' +
            ", active='" + active + '\'' +
            ", added='" + added + '\'' +
            ", updated='" + updated + '\'' +
            '}';
}

}

and this is my adapter public class CountryAdapter extends RecyclerView.Adapter implements Filterable {

public List<Country> countries, countryFiltered;
public Context context;
public AdapterListener adapterListener;
public CountryFilter countryFilter;
private ColorGenerator colorGenerator = ColorGenerator.MATERIAL;

public CountryAdapter(List<Country> countries, Context context, AdapterListener adapterListener) {
    this.context = context;
    this.countries = countries;
    this.countryFiltered = countries;
    this.adapterListener = adapterListener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.country_item, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    TextDrawable drawable = TextDrawable.builder()
            .beginConfig()
            .textColor(Color.BLACK)
            .useFont(Typeface.DEFAULT)
            .fontSize(80)
            .bold()
            .toUpperCase()
            .endConfig()
            .buildRound(countries.get(position).getName().substring(0, 1), colorGenerator.getColor(countries.get(position).getName()));
    viewHolder.getImageView().setImageDrawable(drawable);
    viewHolder.getTvName().setText(countries.get(position).getName());
    viewHolder.getTvCode().setText(countries.get(position).getPhone_code());
}

@Override
public int getItemCount() {
    return countries.size();
}

@Override
public Filter getFilter() {
    if (countryFilter == null) {
        countryFilter = new CountryFilter(countryFiltered, this);
    }
    return countryFilter;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    AppCompatImageView imageView;
    AppCompatTextView tvName, tvCode;

    ViewHolder(View itemView) {
        super(itemView);
        tvName = itemView.findViewById(R.id.tvName);
        tvCode = itemView.findViewById(R.id.tvCode);
        imageView = itemView.findViewById(R.id.imageView);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        adapterListener.onSelected(countries.get(getAdapterPosition()));
    }

    private AppCompatImageView getImageView() {
        return imageView;
    }

    private AppCompatTextView getTvName() {
        return tvName;
    }

    private AppCompatTextView getTvCode() {
        return tvCode;
    }
}

}

Thanks again