无法在列表视图中使进度条不可见

时间:2014-05-06 10:22:38

标签: android android-layout android-listview

这是我的自定义适配器的代码 我在FrameLayout中有一个imageView和一个进度条。我想在下载图像后使进度条不可见..我从Web服务下载图像。在下载图像后调用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");
}
}

2 个答案:

答案 0 :(得分:2)

您需要在GetView中设置可见性。这是因为滚动列表视图会将列表视图中的视图重新分配给新行,如果您在给定行上设置可见性,则当该项移动到新行时将不会更新。请注意,这是双向的 - 如果你想要显示它,你需要明确地使它可见,如果你想让它消失,你需要明确地使它不可见。

答案 1 :(得分:-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");
}
}