Search code examples
androidandroid-recyclerviewtextviewprefetch

Prefetch Text Layout in RecyclerView, cannot resolve method setTextFuture


I'm trying to add Prefetch Text Layout to my reyclerview as shown here and here. Weirdly, it is only working on a my custom TextView and not with standard TextViews. For my standard TextView, I get Cannot resolve method setTextFuture error.

Also to add, everything was working fine before. the standard setText method worked for either custom or regular TextView. I have a total of four (4) TextViews, and all return the same error. I cast one as a custom TextView (even though it wasn't) and the error went away.

Here's my cleaned up code.

@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder rvh, int position){
    rvh.setMtbCard(listFiltered_main.get(position));
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    public TextView tv_name;
    public LengthTextView tv_length;

    public RecyclerViewHolder(final View itemView, final int viewType){
        super(itemView);

        tv_name =itemView.findViewById(R.id.tv_name);
        tv_length =itemView.findViewById(R.id.tv_length);

    public void setMtbCard(final TrailsObject trailsObject) {

        //not working... getting "Cannot resolve method setTextFuture" error
        tv_name.setTextFuture(PrecomputedTextCompat.getTextFuture(
                trailsObject.getName_diff(),
                tv_name.getTextMetricsParamsCompat(),
                null)
        );

        //seemingly works fine
        tv_length.setTextFuture(PrecomputedTextCompat.getTextFuture(
                trailsObject.getLength_mi(),
                tv_length.getTextMetricsParamsCompat(),
                null)
        );
    }
}

Solution

  • Use AppCompatTextView instead of TextView

    XML

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv_id"
        />
    

    In Adapter

    import android.support.v7.widget.AppCompatTextView;
    
    public void onBindViewHolder(@NonNull RecyclerViewHolder rvh, int position){
        rvh.tv.setTextFuture(PrecomputedTextCompat.getTextFuture(
                string_text,
                tv.getTextMetricsParamsCompat(),
                null) //or your executor
        );
    }
    
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public final AppCompatTextView tv;
    
        public RecyclerViewHolder(final View itemView, final int viewType){
            super(itemView);
    
            tv =itemView.findViewById(R.id.tv_id);
    
    
             //......
        }
    }