I'm migrating from old ListView
to RecyclerView
in my project. I have decided to use Data Binding to bind list values, but I'm experiencing some problems to set custom text span for my text box.
Please look at this piece of code (this is how it's done with old ListBox adapater):
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
...
Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/fontawesome.ttf");
SpannableStringBuilder textCategory = new SpannableStringBuilder((item != null ? item.getCategoryIconName() : null) + " " + item.getCategoryName());
textCategory.setSpan (new CustomTypefaceSpan("", tf), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
viewHolder.eventCategory.setText(textCategory);
...
}
Basically, my CustomTypeFaceSpan
is used to set multiple fonts for a single TextView
. How to implement the same with Data Binding? Is it possible to use BindingAdapter
for that?
My try:
<variable name="categoryText" type="String"/>
<variable name="data" type="Event"/>
<TextView
android:id="@+id/event_category_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="@{data.categoryIconName + data.categoryName}"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@color/colorEventCategoryText"
app:customFunction="@{categoryText}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Binding adapter:
@BindingAdapter({"customFunction"})
public static void setSpan(TextView textView, String categoryText)
{
Typeface tf = Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/fontawesome.ttf");
SpannableStringBuilder textCategory = new SpannableStringBuilder(categoryText);
textCategory.setSpan (new CustomTypefaceSpan("", tf), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(textCategory);
}
Problem: categoryText
is always null. Any ideas? TextView is bound properly, because without custom function it displays text as it should.
My list adapter:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
{
public List<Event> events = null;
private final ListFragment.OnFragmentInteractionListener mListener;
public ListAdapter(List<Event> events, ListFragment.OnFragmentInteractionListener mListener)
{
this.events = events;
this.mListener = mListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.cell, viewGroup, false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position)
{
final Event event = events.get(position);
viewHolder.bind(event);
viewHolder.binding.getRoot().setOnClickListener(v ->
{
if (null != mListener)
{
mListener.onFragmentInteraction(event);
}
});
}
@Override
public int getItemCount()
{
return events.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
private final ViewDataBinding binding;
public ViewHolder(ViewDataBinding binding)
{
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj)
{
binding.setVariable(BR.data,obj);
binding.executePendingBindings();
}
}
}