android - 关于ListView的问题
PHP中文网
PHP中文网 2017-04-17 14:52:13
0
1
340

上面图片中展示的是通过ListView加载一些下载列表,现在遇到一个问题:当点击5035这条数据下载时,把列表滚到第二页相应位置的2794113这条数据也会跟着改变,后面每一页的对应位置的数据都会这样,请问该怎么解决?

代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.activity_down_site_item, null);
            viewHolder.siteText = (TextView) convertView.findViewById(R.id.site_name);
            viewHolder.siteBar = (ProgressBar) convertView.findViewById(R.id.site_bar);
            viewHolder.siteDownload = (Button) convertView.findViewById(R.id.site_download);
            viewHolder.siteDelete = (Button) convertView.findViewById(R.id.site_delete);
            viewHolder.siteUpdate = (Button) convertView.findViewById(R.id.site_update);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        
        //从集合中获取数据
        JSONObject jsonObject = (JSONObject) mJsonArray.get(position);
        
        final ViewHolder currentViewHolder = viewHolder;
        
        viewHolder.siteText.setText(jsonObject.get("ID").toString());
        viewHolder.siteDownload.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    currentViewHolder.siteDownload.setText("等待中");
                    currentViewHolder.siteDownload.setClickable(false);
                    currentViewHolder.siteBar.setVisibility(View.VISIBLE);
                    //TODO 下载逻辑
                }
            });
        
        if(isDone==1){
            //隐藏删除、更新按钮
            viewHolder.siteDownload.setVisibility(View.VISIBLE);
            viewHolder.siteDelete.setVisibility(View.GONE);
            viewHolder.siteUpdate.setVisibility(View.GONE);
        }else{
            //隐藏下载按钮
            viewHolder.siteDownload.setVisibility(View.GONE);
            viewHolder.siteDelete.setVisibility(View.VISIBLE);
            viewHolder.siteUpdate.setVisibility(View.VISIBLE);
        }
            return convertView;
        }
PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(1)
阿神

復用的問題

viewHolder.siteDownload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            currentViewHolder.siteDownload.setText("等待中");
            currentViewHolder.siteDownload.setClickable(false);
            currentViewHolder.siteBar.setVisibility(View.VISIBLE);
            //TODO 下载逻辑
        }
    });

分析錯誤原因:

  1. 點擊後設定文字為「等待中」

  2. 滑動時復用先前convertView的siteDownload文字和siteBar的Visibility。
    另外isDone為成員變量,這裡很容易出問題

建議的資料集實作方式
Sample Bean:
int status; // 未開始下載,下載中,已完成下載三個狀態
int progress; // 進度
String downloadID ; // 下載ID
...etc.

根據status 設定對應控制項顯示。偽代碼如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = View.inflate(mContext, R.layout.item_picking_task, null);
        holder = new ViewHolder();
        initView(holder, convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    initListener(position, holder); // 监听
    fillData(position, holder);

    return convertView;
}

private void initView(ViewHolder holder, View convertView) {
    holder.siteText = (TextView) convertView.findViewById(R.id.site_name);
    holder.siteBar = (ProgressBar) convertView.findViewById(R.id.site_bar);
    holder.siteDownload = (Button) convertView.findViewById(R.id.site_download);
    holder.siteDelete = (Button) convertView.findViewById(R.id.site_delete);
    holder.siteUpdate = (Button) convertView.findViewById(R.id.site_update);
}

private void initListener(int position, final ViewHolder holder) {
    final Bean bean = (Bean) mDataList.get(position);
    holder.siteDownload.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            改变状态 bean.status = 1;
            Adapter.this.notifyDataSetChanged(); // 刷新
        }
    });
}

private void fillData(int position, ViewHolder holder) {
    Bean bean = (Bean) mDataList.get(position);
    holder.siteText.setText(bean.downloadID);
    holder.siteDownload.setText(bean.status);  // 伪代码 此处应为stats对应的String类型文本
    holder.siteDownload.setClickable(bean.status != 1);
    holder.siteBar.setVisibility(bean.status == 1 ? View.VISIBLE : View.GONE);
}

private class Bean implements Serializable {
    int progress;    
    int status;  // 未开始: 0,下载中: 1,已完成: 2
    String downloadID;
    ...
}

若還有問題,請追問

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!