ListView项目在滚动时更改位置

时间:2014-04-07 17:41:29

标签: android listview

我已经实现了一个加载新闻的ListView,但是当我滚动列表时,新闻会改变位置。这是列表

public class ListNewsFragment extends SherlockListFragment{

private ListNewsAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    mAdapter = new ListNewsAdapter(this, app.getAllNews());
    setListAdapter(mAdapter);
    super.onCreate(savedInstanceState);
}
}


public class ListNewsAdapter extends BaseAdapter{
private List<News> news;
private Context mContext;
private LayoutInflater inflater;
private ViewHolder holder;

public ListNewsAdapter(final Fragment c, List<News> news) {
    super();
    this.news = news;
    this.mContext = c.getActivity();
    inflater = LayoutInflater.from(mContext);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    if (v == null) {               
         inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
         holder = new ViewHolder();
         v = inflater.inflate(R.layout.fragment_list_news, null);

         holder.name = (TextView) v.findViewById(R.id.tittle);
         holder.tweet = (TextView) v.findViewById(R.id.news);
         holder.avatar = (ImageView) v.findViewById(R.id.image);


         holder.name.setText(news.get(position).getTitulo());
         holder.tweet.setText(news.get(position).getCopete());
         new ImagefetcherTask(position).execute(holder);

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

}

private class ViewHolder{
    public TextView name, tweet;
    public ImageView avatar;
}

private class ImagefetcherTask extends AsyncTask<ViewHolder, Void, ViewHolder> {

        private Bitmap bitmap;
        int position; 

        public ImagefetcherTask(int position) {
            this.position = position;
        }

        @Override
        protected ViewHolder doInBackground(ViewHolder... params) {
             ViewHolder viewHolder = params[0];

            try{
                bitmap = BitmapFactory.decodeStream((InputStream) new URL("...."+ news.get(position).getImagen().toString()).getContent());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

          return viewHolder;
        }

    @Override
    protected void onPostExecute(ViewHolder result) {
        // TODO Auto-generated method stub
        if (bitmap == null) {
            result.avatar.setImageResource(com.dev.suma_intranet_v1.R.drawable.img_perfil);
        } else {
            result.avatar.setImageBitmap(bitmap);
        }
    }

}

}

4 个答案:

答案 0 :(得分:8)

getView()方法更改为:

Map<Integer, View> views = new HashMap<Integer, View>;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (views.containsKey(position)) {    
         return views.get(position);
    }

     inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
     holder = new ViewHolder();
     View v = inflater.inflate(R.layout.fragment_list_news, null);

     holder.name = (TextView) v.findViewById(R.id.tittle);
     holder.tweet = (TextView) v.findViewById(R.id.news);
     holder.avatar = (ImageView) v.findViewById(R.id.image);

     holder.name.setText(news.get(position).getTitulo());
     holder.tweet.setText(news.get(position).getCopete());
     new ImagefetcherTask(position).execute(holder);

     v.setTag(holder); 
     views.put(position, v);
     return v; 

}

答案 1 :(得分:1)

ImageFetcher声明之后致电您的TextView.seTextelse,以便convertView不是null并且已被回收,您就可以&# 39;重新更新你的行:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        holder = new ViewHolder();
        v = inflater.inflate(R.layout.fragment_list_news, null);

        holder.name = (TextView) v.findViewById(R.id.tittle);
        holder.tweet = (TextView) v.findViewById(R.id.news);
        holder.avatar = (ImageView) v.findViewById(R.id.image);

        v.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(news.get(position).getTitulo());
    holder.tweet.setText(news.get(position).getCopete());
    new ImagefetcherTask(position).execute(holder);
    return v;
}

答案 2 :(得分:1)

我通过在我的适配器类中覆盖这两个方法来解决这个问题。

@Override
public int getViewTypeCount() {
   return getCount();
}

@Override
public int getItemViewType(int position) {
   return position;
}

答案 3 :(得分:0)

尝试使用this通用图像加载程序库来延迟加载图像。并尝试捕捉图像。

因此根据this回答将位图选项设置为

DisplayImageOptions  options = new DisplayImageOptions.Builder()
                        .showImageOnLoading(R.drawable.ic_stub)
                        .showImageForEmptyUri(R.drawable.ic_empty)
                        .showImageOnFail(R.drawable.ic_error)
                        .cacheInMemory(true)
                        .cacheOnDisc(true)
                        .considerExifParams(true)
                        .build();

我相信这会解决您的问题