2014-05-06 87 views
0

這是我的自定義適配器代碼 我在FrameLayout中有一個imageView和一個progressbar。我想在圖片下載完成後隱藏進度條。我正在從網絡服務下載圖片。 finishImg()方法在圖像下載後調用。無法使進度條在列表視圖中隱藏

public class CustomAdapter extends BaseAdapter { 
Context mContext; 
ArrayList<ManagePartResponse> partList; 
LayoutInflater mInflater; 
static ViewHolder vh; 

public CustomAdapter(Context context, ArrayList<ManagePartResponse> itemList) { 
    mContext = context; 
    partList = itemList; 
    mInflater = LayoutInflater.from(context); 
} 

public CustomAdapter(ManagePartListActivity managePartListActivity, 
     ArrayList<ManagePartResponse> list) { 
    // TODO Auto-generated constructor stub 

} 

public CustomAdapter() { 

} 

static class ViewHolder { 
    TextView make_tv, model_tv, partName_tv, partId_tv, year_tv; 
    ImageView imageView; 
    ProgressBar pb; 

} 

public int getCount() { 
    // TODO Auto-generated method stub 
    return partList.size(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    if (convertView == null) { 
     vh = new ViewHolder(); 
     convertView = mInflater.inflate(R.layout.row_3, parent, false); 
     vh.make_tv = (TextView) convertView.findViewById(R.id.makeId); 
     vh.model_tv = (TextView) convertView.findViewById(R.id.modelId); 
     vh.partName_tv = (TextView) convertView 
       .findViewById(R.id.partNameId); 
     vh.partId_tv = (TextView) convertView.findViewById(R.id.partId); 
     vh.year_tv = (TextView) convertView.findViewById(R.id.yearId); 
     vh.imageView = (ImageView) convertView 
       .findViewById(R.id.imageView1); 
     vh.pb = (ProgressBar) convertView.findViewById(R.id.progressBar1); 

     convertView.setTag(vh); 
    } else { 
     vh = (ViewHolder) convertView.getTag(); 
    } 
    ManagePartResponse objBean = partList.get(position); 
    vh.make_tv.setText(objBean.getMake()); 
    vh.model_tv.setText(objBean.getModel()); 
    vh.partName_tv.setText(objBean.getPartName()); 
    vh.year_tv.setText(objBean.getYear()); 
    vh.partId_tv.setText(objBean.getStockref()); 
    vh.imageView.setImageBitmap(objBean.getImage()); 

    return convertView; 
} 

public static void finishImg() { 

    vh.pb.setVisibility(View.INVISIBLE); 
    vh.pb.setVisibility(View.GONE); 

    System.out.println("progress bar should be invisible"); 
} 
} 
+0

加布是正確的,以獲得更多關於列表視圖閱讀此博客: http://mindfiremobile.wordpress.com/2014/03/27/optimized-listview-具有偵聽器內最行項/ –

回答

2

您需要在GetView中設置可見性。這是因爲滾動列表視圖會將列表視圖中的視圖重新分配給新行,並且如果您在給定行上設置了可見性,那麼當該項目移動到新行時,它將不會更新。請注意,這是雙向的 - 如果你想顯示它,你需要明確地使其可見,如果你想它消失,你需要明確地使它看不見。

-1

這是它的工作對我來說

public class CustomAdapter extends BaseAdapter { 
Context mContext; 
ArrayList<ManagePartResponse> partList; 
LayoutInflater mInflater; 
static ViewHolder vh; 
static boolean flag=false; 

public CustomAdapter(Context context, ArrayList<ManagePartResponse> itemList) { 
    mContext = context; 
    partList = itemList; 
    mInflater = LayoutInflater.from(context); 
} 

public CustomAdapter(ManagePartListActivity managePartListActivity, 
     ArrayList<ManagePartResponse> list) { 
    // TODO Auto-generated constructor stub 

} 

public CustomAdapter() { 

} 

static class ViewHolder { 
    TextView make_tv, model_tv, partName_tv, partId_tv, year_tv; 
    ImageView imageView; 
    ProgressBar pb; 

} 

public int getCount() { 
    // TODO Auto-generated method stub 
    return partList.size(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    if (convertView == null) { 
     vh = new ViewHolder(); 
     convertView = mInflater.inflate(R.layout.row_3, parent, false); 
     vh.make_tv = (TextView) convertView.findViewById(R.id.makeId); 
     vh.model_tv = (TextView) convertView.findViewById(R.id.modelId); 
     vh.partName_tv = (TextView) convertView 
       .findViewById(R.id.partNameId); 
     vh.partId_tv = (TextView) convertView.findViewById(R.id.partId); 
     vh.year_tv = (TextView) convertView.findViewById(R.id.yearId); 
     vh.imageView = (ImageView) convertView 
       .findViewById(R.id.imageView1); 
     vh.pb = (ProgressBar) convertView.findViewById(R.id.progressBar1); 

     vh.pb.setVisibility(convertView.VISIBLE); 



     convertView.setTag(vh); 
    } else { 
     vh = (ViewHolder) convertView.getTag(); 
    } 
    ManagePartResponse objBean = partList.get(position); 
    vh.make_tv.setText(objBean.getMake()); 
    vh.model_tv.setText(objBean.getModel()); 
    vh.partName_tv.setText(objBean.getPartName()); 
    vh.year_tv.setText(objBean.getYear()); 
    vh.partId_tv.setText(objBean.getStockref()); 
    vh.imageView.setImageBitmap(objBean.getImage()); 

    if(flag){ 
     vh.pb.setVisibility(convertView.INVISIBLE); 
    } 
    return convertView; 
} 

public static void finishImg() { 

    flag=true; 
    System.out.println("progress bar should be invisible"); 
} 
}