【发布时间】: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