2016-02-26 67 views
1

我是新来的在Android的Recycler视图,我正在工作的样式文本,所以我做了这样的下面是我的适配器类。在Recycler视图项目中应用字体后滚动滞后

 public class MyAdapter extends RecyclerView.Adapter { 
    private String[] mDataset; 
    private ArrayList mContactData = new ArrayList(); 
    Context context; 

    // Provide a reference to the views for each data item 
    // Complex data items may need more than one view per item, and 
    // you provide access to all the views for a data item in a view holder 
    public static class ViewHolder extends RecyclerView.ViewHolder { 
     // each data item is just a string in this case 
     public TextView mPhoneNumber; 
     public TextView mContactName; 
     public TextView mCallDuration; 
     public TextView mCallType; 
     private TextView mCallTime; 
     public ViewHolder(View v) { 
      super(v); 
      mPhoneNumber = (TextView) v.findViewById(R.id.phone_number); 
      mContactName = (TextView) v.findViewById(R.id.name_text); 
      mCallDuration = (TextView) v.findViewById(R.id.call_duration_text); 
      mCallType = (TextView) v.findViewById(R.id.call_type); 
      mCallTime = (TextView) v.findViewById(R.id.date_time_text); 
     } 
    } 

    // Provide a suitable constructor (depends on the kind of dataset) 
    public MyAdapter(ArrayList mContactData,Context context) { 
     this.mContactData = mContactData; 
     this.context = context; 
    } 

    // Create new views (invoked by the layout manager) 
    @Override 
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     // create a new view 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_layout, parent, false); 
     // set the view's size, margins, paddings and layout parameters 

     ViewHolder vh = new ViewHolder(v); 
     return vh; 
    } 

    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 
     Typeface typeface_rMedium = Typeface.createFromAsset(context.getAssets(), "Roboto-Medium.ttf"); 
     Typeface typeface_rLignt = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf"); 
     holder.mContactName.setText(mContactData.get(position).getContactName()); 

     holder.mPhoneNumber.setText(mContactData.get(position).getContactNumber()); 
     holder.mPhoneNumber.setTypeface(typeface_rMedium); 
     holder.mCallDuration.setText(mContactData.get(position).getCallDuration()+" sec"); 
     holder.mCallDuration.setTypeface(typeface_rLignt); 
     holder.mCallTime.setText(Utility.getFromatedDateTime(Long.parseLong(mContactData.get(position).getCallTime()))); 
     holder.mCallTime.setTypeface(typeface_rLignt); 
     if(mContactData.get(position).getContactType().equalsIgnoreCase("STD")){ 
      holder.mCallType.setText("S"); 
     }else{ 
      holder.mCallType.setText("L"); 
     } 

    } 

    // Return the size of your dataset (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
     return mContactData.size(); 
    } 
}

之后滚动得到滞后,所以有人猜测出了什么wrong.Thanks提前。

回答

2

尝试创建字体,如下面的ViewHolder构造设置字体:

public ViewHolder(View v) { 
     super(v); 
     mPhoneNumber = (TextView) v.findViewById(R.id.phone_number); 
     mContactName = (TextView) v.findViewById(R.id.name_text); 
     mCallDuration = (TextView) v.findViewById(R.id.call_duration_text); 
     mCallType = (TextView) v.findViewById(R.id.call_type); 
     mCallTime = (TextView) v.findViewById(R.id.date_time_text); 
     // Add typeface lines here and remove below lines from onBindViewHolder() Method 
     Typeface typeface_rMedium = Typeface.createFromAsset(context.getAssets(), "Roboto-Medium.ttf"); 
     Typeface typeface_rLignt = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf"); 
     mPhoneNumber.setTypeface(typeface_rMedium); 
     mCallDuration.setTypeface(typeface_rLignt); 
     mCallTime.setTypeface(typeface_rLignt); 
    } 

删除您已经从onBindViewHolder()方法添加这些行。由于项目视图被重用,因此不需要一次又一次地设置字体。

这里的问题可能是您正在创建字体并设置它的每个项目。 希望这有助于。

+0

谢谢Akshay它的工作。 – leo