【问题标题】:How to set fontStyle for each item in recyclerview如何为recyclerview中的每个项目设置fontStyle
【发布时间】:2021-07-19 05:30:49
【问题描述】:

我有一个 ArrayList 字体,每个项目都有一个写在 textview 中的“文本”,所以我试图为 Recyclerview 中的每个项目设置不同的字体样式。如何为每个项目设置字体样式。

//It's My Adapter class

public class FontAdapter extends RecyclerView.Adapter<FontAdapter.FontViewHolder>
 {
ArrayList<FontModel> fontModelArrayList;
Context context;
int row_index=-1;

public FontAdapter(ArrayList<FontModel> fontModelArrayList, Context context) {
    this.fontModelArrayList = fontModelArrayList;
    this.context = context;
}

@Override
@NonNull
public FontViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    context= parent.getContext();
    LayoutInflater layoutInflater=LayoutInflater.from(context);
    View fontView= layoutInflater.inflate(R.layout.font_list_item,parent,false);
    return new FontViewHolder(fontView);
}

@Override
public void onBindViewHolder(@NonNull FontAdapter.FontViewHolder holder, int position)
{
    FontModel fontModel=fontModelArrayList.get(position);

    holder.itemView.setOnClickListener(v -> {
     row_index=position;
     notifyDataSetChanged();
 });

    // for setting the background
    // when you selected
    if(row_index==position){
        holder.fontLayout.setBackgroundResource(R.drawable.font_background_item2);
        holder.fontTextView.setTextColor(Color.parseColor("#0F393B"));
    }
    else
    {
        holder.fontLayout.setBackgroundResource(R.drawable.font_background_item);
        holder.fontTextView.setTextColor(Color.parseColor("#FFFFFFFF"));
    }
 }

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

public static class FontViewHolder extends RecyclerView.ViewHolder
{
   TextView fontTextView;
   LinearLayout fontLayout;
    public FontViewHolder(@NonNull View itemView)
    {
        super(itemView);
        fontTextView=itemView.findViewById(R.id.fontStyleTextView);
        fontLayout= itemView.findViewById(R.id.fontItemLinearLayout);
    }
 }

}

并且我将所有字体样式放在这样的字体文件夹中。 font/arabic.ttf 等等。如何为每个项目设置不同的字体样式。

 public static ArrayList<FontModel> getAllFonts(){
    ArrayList<FontModel> listOfFonts =new ArrayList<>();
    listOfFonts.add(new FontModel(R.font.arabic));
     ..
     ..
     ..
     ..        
    so on
    return listOfFonts;
}

【问题讨论】:

  • 展示你已经拥有的东西。当您只需要提示在哪里放置 setTypeface 方法调用时,没有人会为您提供整个适配器示例...
  • @snachmsm 好的,谢谢!
  • @snachmsm 我更新了代码,请检查一下。

标签: java android android-recyclerview


【解决方案1】:

你必须在onBindViewHolder末尾添加一些字体设置代码

@Override
public void onBindViewHolder(@NonNull FontAdapter.FontViewHolder holder, int position) {
    FontModel fontModel = fontModelArrayList.get(position);

    // current code

    Context ctx = holder.itemView.getContext();
    Typeface typeface = ctx.getResources().getFont(fontModel.getFontResourceId());
    //or
    Typeface typeface = ResourcesCompat.getFont(ctx, fontModel.getFontResourceId());
    fontTextView.setTypeface(typeface);
}

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多