0

我遇到了一个奇怪的问题,我无法弄清楚为什么我的 ViewPager 不会实例化。根据各种SO问题和教程,我做得正确,但是当我打印到堆栈时,即使我正在调用 setAdapter(adapter)在我的父片段的 onCreateView 中。ViewPager 上方的所有内容都可以正常膨胀,但 ViewPager 只显示空白。

这是代码。

public class Profile extends Fragment {

ListView userInfo;
FeedAdapter adapter;
ViewPager pager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.user_profile, container, false);
    ProfilePagerAdapter adapter = new ProfilePagerAdapter();
    pager = (ViewPager) view.findViewById(R.id.profileViewPager);
    pager.setAdapter(adapter);
    pager.setCurrentItem(0);

    TextView userName = (TextView) view.findViewById(R.id.userName);
    userName.setText(Utility.userName);

    Log.i("Profile", "Fragment and Pager instantiated");

    return view;
}


private class ProfilePagerAdapter extends PagerAdapter{

    public ProfilePagerAdapter(){

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;

        switch(position){
        case 0:
            resId = R.layout.news_feed;
            View view1 = inflater.inflate(resId,null);

            ListView feedList;
            FeedAdapter feedAdapter;

            feedList = (ListView) view1.findViewById(R.id.feedList);
            feedAdapter = new FeedAdapter(getActivity(), Utility.newsFeed);
            feedAdapter.notifyDataSetChanged();
            feedList.setAdapter(feedAdapter);
            ((ViewPager) container).addView (view1,0);
            Log.i("Profile", "Instantiated case 0");
            return view1;
        case 1:
            resId = R.layout.splash;
            View view2 = inflater.inflate(resId,null);
            ((ViewPager) container).addView(view2,0);
            Log.i("Profile", "Instantiated case 1");
            return view2;
        }

        return resId;


    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

}

}

这是xml布局。

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/coverPhoto"
        android:layout_width="match_parent"
        android:layout_height="133dp"
        android:layout_alignParentTop="true"
        android:background="@color/grey_color" />

    <ImageView
        android:id="@+id/profilePhoto"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="100dp"
        android:background="@color/com_facebook_blue" />

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/coverPhoto"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/profilePhoto"
        android:text="User&apos;s Name"
        android:textSize="24sp" />

    <LinearLayout
        android:id="@+id/profileButtonBar"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_below="@id/profilePhoto"
        android:layout_marginTop="5dp"
        android:background="#33b5e5"
        android:orientation="horizontal" >
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/profileViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/profileButtonBar"
        android:layout_marginTop="5dp"
        android:layout_weight="1" />




</RelativeLayout>

4

1 回答 1

3

这是因为您告诉它您有 0 个项目:

@Override
public int getCount() {
    return 0;
}
于 2013-01-05T05:57:57.750 回答