android - ViewPager(带TabHost)片段GetItem被多次调用

时间:2013-10-15 10:21:41

标签: android android-listview android-viewpager android-tabhost android-arrayadapter

我花了几个小时搜索关于我的具体问题的一些有用的帖子但没有成功。

所有帖子都谈到将列表视图布局设置为“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 个答案:

答案 0 :(得分:-1)

而不是fragmentPagerAdapter尝试fragmentStatePagerAdapter