1

事情是这样的:加载此布局时,我的 fragmentpageradapter 在纵向上正常工作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        app:linePosition="top"
        app:selectedColor="#000000" />

</LinearLayout>

它仅在需要时才懒惰地实例化项目

但是,当我切换到横向并加载此布局时(基本上只是在右侧放置一个列表):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <com.viewpagerindicator.TitlePageIndicator
            android:id="@+id/indicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dip"
            app:linePosition="top"
            app:selectedColor="#000000" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="@color/light_grey" />

    <ListView
        android:id="@+id/listStoryView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

事情变得一团糟

我的适配器实例化了所有 30 个项目!并且在每个页面上更改所有 30 个片段都会调用它们的 onCreateView。

这太疯狂了!我究竟做错了什么?

这是我的适配器代码:

package ie.breakingnews.mobile.adapters;

import ie.breakingnews.mobile.R;
import ie.breakingnews.mobile.fragments.ArticleFragment;
import ie.landmarkdigital.shared.models.Article;

import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

public class ArticlePagerAdapter extends FragmentPagerAdapter {

    private final List<Article> articles;
    private final boolean hideTime;
    private int selected;

    private final String of;
    private final String prev;
    private final String next;

    private final String ads;

    public ArticlePagerAdapter(FragmentManager fm, List<Article> articles, boolean hideTime, Context context, String ads) {
        super(fm);

        this.articles = articles;
        this.hideTime = hideTime;

        this.of = context.getString(R.string.of).toUpperCase(Locale.ENGLISH);
        this.prev = context.getString(R.string.previous).toUpperCase(Locale.ENGLISH);
        this.next = context.getString(R.string.next).toUpperCase(Locale.ENGLISH);

        this.ads = ads;
    }

    @Override
    public Fragment getItem(int arg0) {
        Log.e("TCH", "creating an articlefragment for position: " + arg0);
        return ArticleFragment.newInstance(articles.get(arg0), hideTime, ads);
    }

    @Override
    public int getCount() {
        return articles != null ? articles.size() : 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == selected) {
            return String.format(Locale.getDefault(), "%d %s %d", position + 1, of, getCount());
        } else if (position + 1 == selected) {
            return prev;
        } else if (position - 1 == selected) {
            return next;
        }
        return super.getPageTitle(position);
    }

    public void setSelected(int selected) {
        this.selected = selected;
    }
}
4

1 回答 1

2

在您的第一个布局中,ViewPager的父级的宽度为match_parent,看起来是正确的。

在您的横向布局中,ViewPager的父级的宽度为0dp. 这会导致所有项目都被实例化的问题吗?

于 2013-07-08T12:29:10.120 回答