2014-04-07 167 views
0

我已經實現了一個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); 
     } 
    } 

} 

}

+5

只設置文本的TextView當畫面爲空,更好地利用延遲加載加載圖像。也緩存他們,所以你不必下載evertime – Raghunandan

+0

@Raghunandan我應該怎麼做? –

+0

在getView方法中的else語句後移動設置爲文本代碼 –

回答

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; 

} 
+0

如果我這樣做,圖像不斷變化 –

+0

這是因爲你的'ImagefotherTask'。你可以添加代碼嗎?我的猜測是它在滾動並重新使用'View'後更新圖像。如果列表中的項目數量有限,則可以停止重複使用「視圖」,並且它應該可以解決問題。您可能需要考慮將所有'View'存儲在'List'中。 – Tautvydas

+0

這就是我所有的代碼,是的,我有30個項目,我該如何停止重用視圖? –

1

打電話給你的ImageFetcherTextView.seTextelse後聲明,以便當convertViewnull並已回收,您更新了一行:

@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; 
} 
+0

也請寫一些解釋,以便他更好地理解'TableView'中'Views'的回收/可重用性 –

+0

@adneal如果我這樣做,圖像不斷變化 –

+0

@IgnacioGarat這可能與你的ImageFetcher有關。 – adneal

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(); 

我相信這將解決你的問題