2015-10-27 2 views
1

Я использую RecyclerView для заполнения данных из облака. Но похоже, что настраивать шрифт для TextView не так просто, как его внутри представления. Традиционный способ, это то, что я пробовал:Как настроить пользовательский шрифт для TextView в RecyclerView?

TextView tx = (TextView)findViewById(R.id.person_age); 

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Dancing Script.ttf"); 

    tx.setTypeface(custom_font); 

Но вот, он выходит из строя приложения, поэтому ищет исправить, вот код на главной Activiy:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Parse.initialize(this, "app-id", "clientkey"); 
    setContentView(R.layout.activity_big_board); 
    initializeData(); 
    TextView tx = (TextView)findViewById(R.id.person_age); 

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Dancing Script.ttf"); 

    tx.setTypeface(custom_font); 
    adapter = new RVAdapter(persons); 


    rv=(RecyclerView)findViewById(R.id.rv); 

    LinearLayoutManager llm = new LinearLayoutManager(this); 
    rv.setLayoutManager(llm); 
    rv.setHasFixedSize(true); 


    initializeAdapter(); 

} 

private void initializeData(){ 
    persons = new ArrayList<>(); 


    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("BigBoard"); 
    query.findInBackground(new FindCallback<ParseObject>() { 
     public void done(List<ParseObject> credentialList, ParseException e) { 
      if (e == null) { 
       for(int i=0;i<credentialList.size();i++) 
       { 
        a=credentialList.get(i).getString("Location"); 
        b=credentialList.get(i).getString("Feed"); 
        persons.add(new Person(a,b)); 



        Log.d("OUT", "So the Val::------> " +a +b); 


       } 
      } else { 
       Log.d("score", "Error: " + e.getMessage()); 
      } 

      adapter.notifyDataSetChanged(); 
     } 
    }); 


} 

private void initializeAdapter(){ 

    rv.setAdapter(adapter); 
} 

А вот код для класса адаптера:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> { 

public static class PersonViewHolder extends RecyclerView.ViewHolder { 

    CardView cv; 
    TextView personName; 
    TextView personAge; 
    //ImageView personPhoto; 

    PersonViewHolder(View itemView) { 
     super(itemView); 
     cv = (CardView)itemView.findViewById(R.id.cv); 
     personName = (TextView)itemView.findViewById(R.id.person_name); 
     personAge = (TextView)itemView.findViewById(R.id.person_age); 
     //personPhoto = (ImageView)itemView.findViewById(R.id.person_photo); 
    } 
} 

List<Person> persons; 

RVAdapter(List<Person> persons){ 
    this.persons = persons; 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

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

@Override 
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) { 
    personViewHolder.personName.setText(persons.get(i).name); 
    personViewHolder.personAge.setText(persons.get(i).surname); 
    // personViewHolder.personPhoto.setImageResource(persons.get(i).photoId); 
} 

@Override 
public int getItemCount() 
{ 
    if(persons!=null) 
    { 
     return persons.size(); 
    } 
    else 
    { 
     return 0; 
    } 
} 

}

Итак, что это лучший способ сделать это, не вызывая каких-либо Prob лемы к теме интерфейса? Спасибо,

+0

«он выходит из строя приложение »- используйте LogCat для проверки трассировки стека Java, связанной с вашей аварией: https://stackoverflow.com/questions/23353173/unappro-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

Я думал, что это wasn Не требуется. Это 'NullPointerException', поскольку я предполагаю, что он пытается установить шрифт для чего-то еще не готового, – OBX

+0

Вопрос в том, должен ли я выбрать его в классе адаптера? Правильно? – OBX

ответ

2

Перемещайте Typeface логики в PersonViewHolder, вероятно, в его конструктор, по двум причинам:

  1. TextView существует в этой точке.

  2. RecyclerView Ваш, вероятно, будет иметь более чем один PersonViewHolder, в этом случае вам нужно установить Typeface в каждыйPersonViewHolder, потому что там будет больше, чем один пункт и более чем один TextView, что нуждается в Typeface.

+0

Ну, я попробовал это до публикации здесь, но тогда проблема была в 'getAssets()', не может решить метод getAssets(), любая идея? :) – OBX

+0

@oblivion: 'getAssets()' это метод в 'Context'. Вызовите 'getContext()' в 'TextView', чтобы получить« Context ». – CommonsWare

+0

Вы имели в виду замену 'getAssets()' на 'getContext()'?, То опять же та же ошибка :( – OBX

-1

создать другой класс, расширить применение

public class yourApplication extends Application { 
public Typeface robotoRegular; 
public Typeface robotoLight; 
public Typeface robotoBold; 
public Typeface robotoMedium; 


@Override 
public void onCreate() { 
    super.onCreate(); 
    robotoRegular = Typeface.createFromAsset(this.getAssets(), getResources().getString(R.string.robotoRegular)); 
    robotoLight = Typeface.createFromAsset(this.getAssets(), getResources().getString(R.string.robotoLight)); 
    robotoBold = Typeface.createFromAsset(this.getAssets(), getResources().getString(R.string.robotoBold)); 
    robotoMedium = Typeface.createFromAsset(this.getAssets(), getResources().getString(R.string.robotoMedium)); 

    } 
} 

затем установите его в связующем

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    final String name = mDataset.get(position); 

    holder.txtHeader.setTypeface(((yourApplication) mContext.getApplicationContext()).robotoRegular); 
    holder.txtFooter.setTypeface(((yourApplication) mContext.getApplicationContext()).robotoLight); 
} 
0
public class Adapter_Recycler_Film extends RecyclerView.Adapter<Adapter_Recycler_Film.DataViewHolder> { 
    private static Typeface face; 
    private Context mContext; 
    Adapter_Recycler_Film.DataViewHolder viewHolder; 

    public Adapter_Recycler_Film(Context context,List<String> dateData) { 
     ........ 
     this.mContext=context; 
     face = Typeface.createFromAsset(mContext.getAssets(),"fonts/IRANSansMobile(FaNum)_Medium.ttf"); 
    } 
} 

public class DataViewHolder extends RecyclerView.ViewHolder { 
    public TextView tDate; 

    public DataViewHolder(View itemView) { 
     super(itemView); 
     tDate = (TextView) itemView.findViewById(R.id.txt); 
     if(tDate!=null){ 
      tDate.setTypeface(face); 
     } 

    } 
} 
0

Как @ CommonsWare отвечают, вероятно, самое непосредственное отношение к вашему конкретному случаю. В перспективе эффективности, удобочитаемости и ретроспективности я бы рекомендовал создать пользовательский TextView, а затем присвоить TextView строке позиции вашего RecyclerView или в любом месте приложения, где вам нужен определенный шрифт.

CustomFontTextView.java

package icn.premierandroid.misc; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.TextView; 

import icn.premierandroid.R; 

public class CustomFontTextView extends TextView { 
    AttributeSet attr; 
    public CustomFontTextView(Context context) { 
     super(context); 
     setCustomFont(context, attr); 
    } 

    public CustomFontTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     String customFont = null; 
     TypedArray a = null; 
     if (attrs != null) { 
      a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView); 
      customFont = a.getString(R.styleable.CustomFontTextView_customFont); 
     } 
     if (customFont == null) customFont = "fonts/portrait-light.ttf"; 
     setCustomFont(ctx, customFont); 
     if (a != null) { 
      a.recycle(); 
     } 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
      tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e("textView", "Could not get typeface", e); 
      return false; 
     } 
     setTypeface(tf); 
     return true; 
    } 
} 

значения/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CustomFontTextView"> 
     <attr name="customFont" format="string"/> 
    </declare-styleable> 
</resources > 

Пример элемент строки для RecyclerView

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    card_view:cardUseCompatPadding="true" 
    card_view:cardCornerRadius="8dp"> 

    /** Containers and other Components **/ 

    <icn.premierandroid.misc.CustomFontTextView 
     android:id="@+id/comment_user_name" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:layout_marginStart="5dp" 
     android:layout_marginLeft="5dp" 
     android:gravity="center_vertical" 
     android:layout_weight="0.36"/> 

    <icn.premierandroid.misc.CustomFontTextView 
     android:layout_width="0dp" 
     android:layout_weight="0.40" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:layout_marginStart="1dp" 
     android:layout_marginLeft="1dp" /> 

    <icn.premierandroid.misc.CustomFontTextView 
     android:id="@+id/comment_time" 
     android:layout_width="0dp" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical" 
     android:layout_height="match_parent"/> 

/** Containers and other Components **/ 

</android.support.v7.widget.CardView> 

Таким образом, вооруженный этим кодом, вам не нужно ничего менять в своих RecyclerView.Adapter, RecyclerView.ViewHolder или в любом месте, где у вас уже есть TextView.

Примечание: вы все еще можете инициализировать компоненты, используя TextView textView = (TextView) findViewById(R.id.textView);. XML делает всю работу за вас. Если вы создаете TextView компоненты программно, то вам нужно будет создать его как CustomFontTextView textView = new CustomFontTextView();

Также вы можете применить ту же логику практически любых компонентов, таких как EditText, Switches, RadioButtons, TabLayouts и т.д. и т.п.

Смежные вопросы