2016-07-26 65 views
0

我有一個非常簡單的RecyclerView,但我想要傳遞給視圖的字符串上的自定義字體。我有一些額外的東西,但你可以忽略那些代碼。這裏是我的適配器:如何在RecyclerView中使用的字符串上使用自定義字體?

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

    } 
} 

}

這是我的數據被保存。我知道我幾次運行同樣的東西,但我只是看到了滾動。不管怎麼說,這裏是代碼:

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

}

如果它是不可能把一個自定義字體上的繩子,什麼話我必須做的,使我的所有數據一個TextView?

謝謝

沙魯克·拉扎

回答

0

您應該創建一個資產文件夾,然後創建子文件夾字體,並把所有的字體到那裏。

在您的Holder類:

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