简体   繁体   中英

How do you use a custom font on Strings that are being used in a RecyclerView?

I have a very simple RecyclerView but I want to put a custom font on the Strings that are being passed to the View. I have a few extra things but you could just ignore that code. Here is my adapter :

public class Adapter extends RecyclerView.Adapter<Adapter.Holder> {

private List<ListItem> ListData;
private LayoutInflater inflater;



private ItemClickCallBack itemClickCallBack;

public interface ItemClickCallBack {
    void onItemClick(int p);
    void onSecondaryClick(int p);
}

public void setItemClickCallBack(final ItemClickCallBack itemClickCallBack){
    this.itemClickCallBack= itemClickCallBack;
}


public Adapter(List<ListItem> ListData, Context context) {
    inflater = LayoutInflater.from(context);
    this.ListData = ListData;

}


@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new Holder(view);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    ListItem item = ListData.get(position);
    holder.title.setText(item.getTitle());
    holder.subtitle.setText(item.getSubTitle());
    if(item.isStar()){
        holder.thumbnail.setImageResource(R.drawable.ic_star_black_24dp);
    }else{
        holder.thumbnail.setImageResource(R.drawable.ic_star_border_black_24dp);
    }
}



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

public void setListData (ArrayList<ListItem> exerciseList){
    this.ListData.clear();
    this.ListData.addAll(exerciseList);
}

class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView title;
    private View container;
    private TextView subtitle;
    private ImageView avatar;
    private ImageView thumbnail;

    public Holder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.textRoot);

        container = itemView.findViewById(R.id.listItem);

        container.setOnClickListener(this);

        subtitle = (TextView) itemView.findViewById(R.id.txt_subtitle);
        thumbnail = (ImageView) itemView.findViewById(R.id.secondaryIcon);

        thumbnail.setOnClickListener(this);

        avatar = (ImageView) itemView.findViewById(R.id.img_avatar);


    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.listItem){
                 itemClickCallBack.onItemClick(getAdapterPosition());
        }else{
              itemClickCallBack.onSecondaryClick(getAdapterPosition());
        }

    }
}

}

This is where my data is saved. I know that I am running the same thing a couple times, but I was just seeing the scrolling. Anyways, here is the code:

public class Data {


final private static String duas[] = {"Hello to you","Bye to you","Walaa"};
final private static String Trans[] = {"Hmmmm", "Bruh","Khalas"};

private final static int face = R.drawable.ic_tag_faces_black_24dp;

public static List<ListItem> getListData() {
    List<ListItem> data = new ArrayList<>();
    for (int x = 0; x < 4; x++) {
        for (int i = 0; i < duas.length; i++) {
            ListItem item = new ListItem();
            item.setTitle(duas[i]);
            item.setSubTitle(Trans[i]);
            data.add(item);

        }
    }
    return data;
}

}

And if it is not possible to put a custom font on a String, what would I have to do to make all my data a TextView?

Thank you,

Shahrukh Raza

You'll need a custom TextView to use custom font.

Please refer to this link: How to make a custom TextView?

You should create an assets folder, then create a sub-folder fonts and put all your fonts into there.

In your Holder class:

TextView title = (TextView) itemView.findViewById(R.id.textRoot);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/your_font_name.ttf");
title.setTypeface(face);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM