gpt4 book ai didi

android - FragmentPagerAdapter 实例化所有 fragment

转载 作者:行者123 更新时间:2023-11-29 21:39:35 25 4
gpt4 key购买 nike

事情是这样的:加载此布局时,我的 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 个 fragment ,调用它们的 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;
}
}

最佳答案

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

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

关于android - FragmentPagerAdapter 实例化所有 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17524289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com