2

我花了幾個小時搜索一些有關我的具體問題的有用的文章沒有成功。android-ViewPager(帶TabHost)片段GetItem被多次調用

所有帖子都將設置列表視圖佈局爲「fill_parent」。但是我的佈局中沒有明確的列表視圖。

下面是代碼:

TabsAdapter:

public class TabsAdapter extends FragmentPagerAdapter implements 
    TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, IconPagerAdapter { 
. 
. 
    @Override 
    public Fragment getItem(int position) { 
     TabInfo info = mTabs.get(position); 
     return Fragment.instantiate(mContext, info.clss.getName(), 
      info.args); 
    } 
. 
. 

ArrayAdapter:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = null; 
    if (convertView == null) { 
     LayoutInflater inflator = context.getLayoutInflater(); 
     view = inflator.inflate(
       R.layout.list_activity_segnalazioni_archivio_riga, null); 
     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.codiceEsterno = (TextView) view 
       .findViewById(R.id.list_activity_segnalazione_archivio_riga_textview_codice_esterno); 
     viewHolder.data = (TextView) view 
       .findViewById(R.id.list_activity_segnalazione_archivio_riga_textview_data); 
     viewHolder.posizione = (TextView) view 
       .findViewById(R.id.list_activity_segnalazione_archivio_riga_textview_posizione); 
     viewHolder.codiceEsterno.setFocusable(false); 
     viewHolder.codiceEsterno.setFocusableInTouchMode(false); 
     viewHolder.data.setFocusable(false); 
     viewHolder.data.setFocusableInTouchMode(false); 
     viewHolder.posizione.setFocusable(false); 
     viewHolder.posizione.setFocusableInTouchMode(false); 


     viewHolder.removeButton = (ImageButton) view.findViewById(R.id.list_activity_segnalazione_archivio_riga_imagebutton_remove); 
     viewHolder.removeButton.setFocusable(false); 
     viewHolder.removeButton.setFocusableInTouchMode(false); 
     viewHolder.previewImage = (ImageView) view 
       .findViewById(R.id.row_image); 

      . 
      . 
      . 
     view.setTag(viewHolder); 
    } else { 
     view = convertView; 
    } 

    ViewHolder holder = (ViewHolder) view.getTag(); 
    try { 
     holder.codiceEsterno.setText(segnalazioni.get(position).getCodiceEsterno()); 
     holder.data.setText(DateUtils.formatToHumanDate(segnalazioni.get(
       position).getDataInizio())); 

     String address = segnalazioni.get(position).getToponimo(); 
     if (segnalazioni.get(position).getCivico() != null 
       && !segnalazioni.get(position).getCivico().equals("")) { 
      address = address.concat(", ").concat(
        segnalazioni.get(position).getCivico().toString() 
        .toUpperCase(Locale.ITALY)); 
     } 
     holder.posizione.setText(address); 

     Map<String, Object> sqlParams = new HashMap<String, Object>(); 
     sqlParams.put("identita", segnalazioni.get(position).getIdStorico()); 

      // This retrieve a base64 image from the sqlite. 
      // Getting called more times that needed gives me an incredible cpu overhead 
      **String thumb64 = SegnalamiApplication.abatis.executeForString("getAllFileDatiByIdEntitaForThumb", sqlParams);** 

      if(thumb64 != null) 
      { 
       holder.previewImage.setImageBitmap(BitmapUtils.base64ToBitmap(thumb64)); 
      } 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
     return view; 
    } 

XML佈局(最重要的不同的東西比其他的GetItem職位):

<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/archivio_pager" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

    </LinearLayout> 
</TabHost> 

在這一點上,每個項目被稱爲5次,這給我一個很高的元素數量(5項意味着25 GetItem調用)的開銷。

我真誠地希望我們一起解決問題。

L.F.

回答

-1

代替fragmentPagerAdapter嘗試fragmentStatePagerAdapter