2014-11-04 74 views
0

描述:我如何更新列表視圖中的單個行?

我正在下載字體文件,當文件被下載時,我使用該文件作爲自定義字體。但是,當第1行更新時,它也會更新第8行,反之亦然。我嘗試了所有的方法,但我無法解決。下面是getView的代碼。請幫我解決這個問題。

在此先感謝。

public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    ViewHolder holder; 
    if(convertView == null) { 
     Log.d("inside value", position + ""); 
     holder = new ViewHolder(); 
     view = inflater.inflate(R.layout.sub_family_item, null); 
     holder.subVariantText = (TextView) view.findViewById(R.id.sub_family_style_text); 
     holder.subTextStyle = (TextView) view.findViewById(R.id.text_style); 
     holder.progressBar = (ProgressBar) view.findViewById(R.id.progress_bar_id); 
     view.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) view.getTag(); 
    } 

    SubFont fontStyle = getItem(position); 
    if(fontStyle != null) 
    { 
     holder.subVariantText.setText(fontStyle.getSubList()); 
     holder.subTextStyle.setText(fontStyle.getFontStyleText()); 
    } 

    File file = new File(FontConstant.folder+"/fontFile" + selectedPosition + position + ".ttf"); 
    if(file.exists() && file.length() == this.fileSize) 
    { 
     TextView textView = (TextView) view.findViewById(R.id.text_style); 
     holder.progressBar.setVisibility(View.INVISIBLE); 
     Typeface custom_font = Typeface.createFromFile(file); 
     holder.subTextStyle.setTypeface(custom_font); 
     holder.subTextStyle.setVisibility(View.VISIBLE); 
    } 
    return view; 
} 

static class ViewHolder 
{ 
    TextView subVariantText; 
    TextView subTextStyle; 
    ProgressBar progressBar; 
} 
+0

holder.subTextStyle.setTypeface(..)將導致修改視圖屬性。但是由於視圖被重用,它將應用於這個特定視圖被回收的行(作爲convertView)..現在,如果您只想應用於特定行,那麼您必須編寫其他case來將typeFace更改爲normal,所以,當視圖重新循環,它得到正常的行爲 – Aun 2014-11-04 05:58:40

回答

1

您需要添加else語句進行getView

if(fontStyle != null) 
{ 
    holder.subVariantText.setText(fontStyle.getSubList()); 
    holder.subTextStyle.setText(fontStyle.getFontStyleText()); 
} 
else 
{ 
    //what should this row display if fontStyle is null? 
} 

if(file.exists() && file.length() == this.fileSize) 
{ 
    TextView textView = (TextView) view.findViewById(R.id.text_style); 
    holder.progressBar.setVisibility(View.INVISIBLE); 
    Typeface custom_font = Typeface.createFromFile(file); 
    holder.subTextStyle.setTypeface(custom_font); 
    holder.subTextStyle.setVisibility(View.VISIBLE); 
} 
else 
{ 
    //what should this row display if the file is not exist? 
} 
0

嘗試這樣可以幫助你,

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if(convertView == null) { 
     Log.d("inside value", position + ""); 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.sub_family_item, null, false); 
     holder.subVariantText = (TextView) convertView.findViewById(R.id.sub_family_style_text); 
     holder.subTextStyle = (TextView) convertView.findViewById(R.id.text_style); 
     holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress_bar_id); 

     convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    SubFont fontStyle = getItem(position); 

    if(fontStyle != null) 
    { 
     holder.subVariantText.setText(fontStyle.getSubList()); 
     holder.subTextStyle.setText(fontStyle.getFontStyleText()); 
    } 

    File file = new File(FontConstant.folder+"/fontFile" + selectedPosition + position + ".ttf"); 

    if(file.exists() && file.length() == this.fileSize) 
    { 
     holder.progressBar.setVisibility(View.INVISIBLE); 
     Typeface custom_font = Typeface.createFromFile(file); 
     holder.subTextStyle.setTypeface(custom_font); 
     holder.subTextStyle.setVisibility(View.VISIBLE); 
    } else{ 
     holder.subTextStyle.setVisibility(View.GONE); 
    } 
    return convertView; 
} 

static class ViewHolder 
{ 
    TextView subVariantText; 
    TextView subTextStyle; 
    ProgressBar progressBar; 
} 

我希望它會幫助你

+0

其實我正在下載不同的字體大小,一旦文件下載我想顯示字體樣式。通過一次使用您的代碼,只顯示一種字體樣式,因爲您將可見性設置爲消失。希望你能理解我的問題。 – Aniruddha07 2014-11-04 06:27:39

1

將數據添加到您的數據的收集傳遞給您的適配器

然後致電

listView.notifyDataSetChanged(); 

多數民衆贊成它。

相關問題