2013-08-21 38 views
1

我希望在我的列表視圖中插入額外的元素。 E.g我有無限的圖像列表視圖,每20張圖片後,我想添加web視圖鏈接到源頁面。如何在listview的項目之間添加元素?

根據sugested 不幸的是收到上WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);

public class MediaItemAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater = null; 
    public ImageLoader imageLoader; 

    public MediaItemAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader = new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 2; // any number what you need. 
    } 

    @Override 
    public int getItemViewType(int position) { 
     // view type is managed as zero-based index. 
     if (position % 11 != 0) 
      return 0; 
     else 
      return 1; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 

     if (convertView == null) { 
      // View Recycling is managed separately based on its view type, 
      // so you don't need to worry about view corruptions. 

      int type = getItemViewType(position); 
      switch (type) { 
      case 0: 
       // inflate imageview here. 
       convertView = inflater.inflate(R.layout.item_composer, null); 
      case 1: 
       // inflate webview here. 
       convertView = inflater.inflate(R.layout.banner_row, null); 
      } 
     } 
     int type = getItemViewType(position); 
     switch (type) { 
     case 0: 
      // inflate imageview here. 

      ImageView thumb_image = (ImageView) vi.findViewById(R.id.imagePrev); // thumb 


      String value = add.get("first_photo_url"); 
      if (value == null || value.trim().length() <= 0 
        || value.equalsIgnoreCase("null") 
        || value.trim().equalsIgnoreCase("")) { 
       thumb_image.setImageResource(R.drawable.stub); 
      } else { 
       imageLoader.DisplayImage(add.get("first_photo_url"), 
         thumb_image); 
       // // do nothing 
      } 
     case 1: 
      // inflate webview here. 
      WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse); 

      WebSettings webSettings = sourse.getSettings(); 
      webSettings.setJavaScriptEnabled(true); 
      // banner.loadUrl("http://www.google.com"); 
      sourse.loadUrl(url); 
     } 

     return vi; 
    } 

} 
+0

也許這將有助於http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ –

回答

2

ListView的支持比一個更ViewType NPE。您可以覆蓋ListAdapter中的getViewTypeCount & getItemViewType方法。在你的情況下,你可以準備兩種視圖類型一個用於圖像,另一個用於webvie,並根據項目的位置單獨使用。例如...

private class MyCustomAdapter extends ArrayAdapter<Object> { 

    public MyCustomAdapter(Context context) { 
     super(context, 0); 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 2; //any number what you need. 
    } 

    @Override 
    public int getItemViewType(int position) { 
     //view type is managed as zero-based index. 
     if(position % 20 != 0) 
      return 0; 
     else 
      return 1; 
    } 

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

     if(convertView == null){ 
      // View Recycling is managed separately based on its view type, 
      // so you don't need to worry about view corruptions. 

      LayoutInflater inflater = getLayoutInflater(); 
      int type = getItemViewType(position); 
      switch (type){ 
       case 0: 
        //inflate imageview here. 
        convertView = inflater.inflate(R.layout.my_imageivew); 
       break; 
       case 1: 
        //inflate webview here. 
        convertView = inflater.inflate(R.layout.my_webview); 
      } 
      //do some data binding job here... 
     } 
     return convertView; 
    } 
}//end of inner class 

而且我發現一個有用的博客文章關於這一點 - 「handling-listviews-with-multiple-row-types

+0

同時我正在閱讀提出的博客,你能想到我爲什麼會在webview初始化中接受NPE嗎? – Yarh

+1

是的,我只是忘了打「break」。在兩起案件之間,似乎你只是跟着我的重大錯誤。 =) – Chansuk

0

在getView方法中的列表適配器,您可以檢查以下條件:

if(position%20==0){ 
    //return your webview here instead of imageView or list row etc. 
} 
相關問題