ViewPager未显示视图

时间:2015-05-15 16:58:20

标签: android android-viewpager android-fragmentactivity

我最近使用FragmentStatePagerAdapter在ViewPager上工作 ,当我在模拟应用程序时,它没有显示当前片段的视图...

这是我的FragmentActivity类:

import android.os.Bundle;
import android.os.PersistableBundle;``
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class CrossFadingActivity extends FragmentActivity {
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private int nbPages = Ipsum.articleContent.length;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle   persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.cross_fading_pager);
    mViewPager=(ViewPager)findViewById(R.id.pager);
    mPagerAdapter=new MyPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mPagerAdapter);

}
private class MyPagerAdapter extends FragmentStatePagerAdapter{
    public MyPagerAdapter(FragmentManager fm){
        super(fm);
    };
    @Override
    public int getCount() {
        return nbPages;
    }

    @Override
    public Fragment getItem(int i) {
        return ArticleFragment.create(i);
    }

}

}

这里是Fragment类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
private int mCurrentPosition;
public static final String TAG_PAGE = "position";
public static Fragment create(int position)
{
    ArticleFragment fragment = new ArticleFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(TAG_PAGE,position);
    fragment.setArguments(bundle);
    return fragment;
}

public ArticleFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCurrentPosition=getArguments().getInt(TAG_PAGE);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {

ViewGroup viewGroup =    (ViewGroup)inflater.inflate(R.layout.article_fagment,
            container,
            false);
    View v = inflater.inflate(R.layout.article_fagment,null);

    ((TextView)viewGroup.findViewById(R.id.articleTitle)).
            setText(Ipsum.articleTitle[mCurrentPosition]);
    ((ImageView)viewGroup.findViewById(R.id.articleImage)).
            setImageResource(Ipsum.articleImage[mCurrentPosition]);
            ;
    ((TextView)viewGroup.findViewById(R.id.articleContent)).
            setText(Ipsum.articleContent[mCurrentPosition]);
    return v;
}

public int getmCurrentPosition()
{
    return mCurrentPosition;
}
}

ViewPager在此布局中设置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/pager"
 />

MainView:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15dp"
                android:text="@string/txt_article_slogan"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:textSize="20dp"
                android:id="@+id/articleTitle"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/txt_article_date_publish"
                android:textSize="10dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/articleImage"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/articleContent"
                android:paddingRight="10dp"
                />
        </LinearLayout>
    </LinearLayout>
    </ScrollView>

2 个答案:

答案 0 :(得分:0)

错误可能在getCount()返回。 确保它没有返回0.

答案 1 :(得分:0)

我遇到了同样的问题。 从FragmentActivity班级中删除PersistableBundleonCreate()中的super()参数。

我不知道为什么,但它对我有用。 希望这对你有帮助。