在RecyclerView中预取文本布局,无法解析方法setTextFuture

时间:2018-12-07 23:59:43

标签: android android-recyclerview textview prefetch

我正在尝试将预取文本布局添加到reyclerview中,如图herehere所示。奇怪的是,它仅适用于我的自定义TextView,而不适用于标准的TextViews。对于我的标准TextView,我收到Cannot resolve method setTextFuture错误。

也要补充一点,之前一切正常。标准setText方法适用于自定义或常规TextView。我总共有四(4)个TextView,并且都返回相同的错误。我将其转换为自定义TextView(即使不是),但错误消失了。

这是我清理的代码。

@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)
        );
    }
}

1 个答案:

答案 0 :(得分:1)

使用AppCompatTextView代替TextView

XML

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

在适配器中

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);


         //......
    }
}