gpt4 book ai didi

android - RecyclerView 自定义字体内部适配器

转载 作者:行者123 更新时间:2023-11-29 19:42:20 24 4
gpt4 key购买 nike

我正在使用 RecyclerView 来显示三个不同的文本,我想为每个文本应用自定义的 Typeface。现在,在我的 ViewHolder 下的适配器中,我已经放置了 Typeface 代码,但不确定如何将它们应用于每个 textView

适配器.java

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

List<AdapterData> mItems;

public Adapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);

data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");

data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);


}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview_items, viewGroup, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.textOne.setText(data.getTextOne());
viewHolder.textTwo.setText(data.getTextTwo());
viewHolder.textThree.setText(data.getTextThree());

}


@Override
public int getItemCount() {

return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

public TextView textOne;
public TextView textTwo;
public TextView textThree;

Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");


public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView)itemView.findViewById(R.id.textView1);
textTwo = (TextView)itemView.findViewById(R.id.textView2);
textThree = (TextView)itemView.findViewById(R.id.textView3);


}
}
}

最佳答案

class ViewHolder extends RecyclerView.ViewHolder {

public TextView textOne;
public TextView textTwo;
public TextView textThree;

Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");


public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView) itemView.findViewById(R.id.textView1);
textOne.setTypeface(customTypeOne);
textTwo = (TextView) itemView.findViewById(R.id.textView2);
textTwo.setTypeface(customTypeTwo);
textThree = (TextView) itemView.findViewById(R.id.textView3);
textThree.setTypeface(customTypeThree);
}
}

关于android - RecyclerView 自定义字体内部适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com