简体   繁体   中英

ViewPager - the code doesn't reach the adapter

This is my code:

.......
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {  Log.i("App", "U reaching Da onCreate");       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_fragment);


     mCollectionPagerAdapter =
                new CollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.CardsPager);
        mViewPager.setAdapter(mCollectionPagerAdapter);
  ....

    //ADAPTER:
class CollectionPagerAdapter extends FragmentStatePagerAdapter {
    public CollectionPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new ObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(ObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        Log.i("App", "U reaching Da Adapter!!");
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}
 .....
//ObjectFragment.java
public class ObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
            R.layout.cards_pager, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            Integer.toString(args.getInt(ARG_OBJECT)));
        Log.i("App", "U reaching Da inflater Y U no work?!");
        return rootView;
    }
}

main_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="40"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#109922" >

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="20"
    android:orientation="vertical" 
    android:background="#115599">

     <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/CardsPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000" >

     </android.support.v4.view.ViewPager>



</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="40"
    android:orientation="vertical" >



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:background="#ff9922">
        </LinearLayout>

</LinearLayout>

It seems like the app never reach the adapter.

I did exactly as the android developers guide. I don't know what's going on here, eclipse let me run the app and everyting seems to be fine in the code but it just no working

我终于弄对了,问题是我在onCreate()方法中设置了适配器,为解决此问题,我在MainFragment的onCreateView()方法中设置了适配器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM