2016-09-27 46 views
0

我已經閱讀並嘗試了幾種類似的問題在堆棧溢出的解決方案,但沒有一個解決了我的問題。這是事情。CustomListAdapter列表項互相模仿

我有一個listview它使用CustomListAdapter,每個列表項有一個progress bar,一個下載按鈕,標題文本等。當點擊下載button時,執行下載操作,並且基於下載的結果(無論成功還是失敗)相關的列表項被更新(UI),例如,如果完全隱藏下載按鈕,更新進程progress bar在下載)

listview顯示四個項目在每一個給定的時間

first four items on display

的問題是,當一個UI變化對項進行說第1項(索引爲0)的第5項也將有相同的改變,同樣如果改變了t第3項,第7項涉及這些變化。總之,項目N + 4總是模仿項N.

changes of item zero, duplicated by item 5

看看我getView()會告訴我檢查了所有已知的盒子。要解決這個

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    ViewHolder holder; 

    if (convertView == null) { 
     view = inflater.inflate(R.layout.item_mylibrarylist, null); 
     holder = new ViewHolder(); 

     holder.name = (TextView)view.findViewById(R.id.name); 
     holder.name.setTypeface(MainActivity.font_bahamas); 

     holder.author = (TextView)view.findViewById(R.id.author); 
     holder.author.setTypeface(MainActivity.font_bahamas); 

     holder.worktype = (TextView)view.findViewById(R.id.worktype); 
     holder.worktype.setTypeface(MainActivity.font_bahamas); 

     holder.coverPic = (TextView)view.findViewById(R.id.coverPic); 
     holder.downloadBt = (TextView)view.findViewById(R.id.downloadBt); 
     holder.progressBar = (ProgressBar)view.findViewById(R.id.progressBar2); 

     holder.menuBt = (ImageView)view.findViewById(R.id.menuBt); 

     holder.position = position; 

     view.setTag(holder); 

    } else { 
     holder = (ViewHolder) view.getTag(); 

    } 

    if(holder.position == position) { 
     setValuesForListItemViews(holder, position, view); 
    } 
    return view; 
} 

設置每個列表項的方法..

private void setValuesForListItemViews(ViewHolder holder, int position, View view) { 

    if (!data.isEmpty()) { 
     // set the list item elements here 
     final CreativeWork creativeWork = data.get(position); 

     holder.name.setText(creativeWork.getName().toLowerCase()); 
     holder.author.setText("by " + creativeWork.getOriginal_authors().toLowerCase()); 
     holder.worktype.setText(creativeWork.getWork_type().toLowerCase()); 

     Drawable draw = res.getDrawable(R.drawable.custom_progressbar2); 
     holder.progressBar.setProgressDrawable(draw); 
     holder.progressBar.setMax(100); 
     holder.progressBar.setVisibility(View.INVISIBLE); 

     holder.menuBt.setOnClickListener(new OnItemClickedListener(view, position, 1, creativeWork, holder.progressBar, holder.downloadBt)); 
     holder.menuBt.setOnCreateContextMenuListener(new MContextMenuListener(creativeWork, holder.progressBar, holder.downloadBt, false)); 


     //load image url 
     ImageLoader2 imgLoader12 = new ImageLoader2(activity); 
     imgLoader12.DisplayImage(creativeWork.getName(), R.drawable.downloads, holder.downloadBt); 

     ImageLoader imgLoader = new ImageLoader(activity); 
     imgLoader.DisplayImage(SLService.END_POINT + creativeWork.getImage_url(), R.drawable.soul_lounge, holder.coverPic); 

     //check if file already exist and switch off download button 
     DBHelper helper = new DBHelper(activity); 
     CreativeWork cw = helper.getCreativeWork(creativeWork); 

     if (cw != null) { 
      File file = new File(cw.getFilePath()); 
      if (file.exists()) { 
       holder.menuBt.setOnCreateContextMenuListener(new MContextMenuListener(creativeWork, holder.progressBar, holder.downloadBt, true)); 

       //check if the file download was complete 
       if (cw != null) { 
        if (cw.getFileSize() > file.length()) { 
         holder.progressBar.setProgressDrawable(activity.getResources().getDrawable(R.drawable.custom_progressbar3)); 

         ImageLoader2 imgLoader2 = new ImageLoader2(activity); 
         imgLoader2.DisplayImage(cw.getName(), R.drawable.restart, holder.downloadBt); 

         holder.progressBar.setProgress((int) ((file.length() * 100)/cw.getFileSize())); 
         holder.progressBar.setVisibility(View.VISIBLE); 
        } else { 
         holder.downloadBt.setVisibility(View.INVISIBLE); 
        } 
       } 
      } 
     } 
     holder.downloadBt.setOnClickListener(new OnItemClickedListener(view, position, creativeWork, holder.progressBar, holder.downloadBt, 0)); 
    } 
} 
+0

請發佈設置視圖代碼[前四個元素和下一個循環] –

回答

1

最簡單的方法是添加一些額外的領域,其名單要傳遞到您的適配器模型類。

boolean showDownloadButton; //default is true 
int progress;// default is 0 

所以,當上下載按鈕(或任何其它期望的事件)用戶點擊在給定位置的showDownloadButtonboolean值更改爲false用於模型對象,並調用adapter.notifyDatasetChanged()和相應地管理按鈕能見度在你的適配器中。

,並一定要確保爲視圖中添加可見光和走了條件藏漢

if(modelList.get(position).getShowDownloadButton()) 
{ 
    btnDownload.setVisibility(View.VISIBLE) 
} 
else 
{ 
    btnDownload.setVisibility(View.GONE) 
} 
0

Nitesh的答案是正確除此之外,還有另一種方式做,如果你不想創建Model類

使用setId和getId的位置來標識唯一行並將該id分配給進度條以獲得解決方案