2013-07-07 30 views
0

這是我的代碼:ViewPager - 代碼沒有達到適配器

....... 
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> 

這似乎是應用從未達到的適配器。

我完全按照android開發人員指南進行操作。 我不知道發生了什麼事情就到這裏,日食讓我跑的應用和everyting似乎是在代碼不錯,但它只是沒有工作

+0

*這似乎應用程序永遠不會到達適配器。* - 這是什麼意思?你還可以發佈佈局文件'main_fragment.xml'嗎? – Luksprog

+0

我發表了評論到logcat「Log.i(」App「,」U達到Da Adapter !!「);」 在適配器類中,它沒有出現在logcat中 – Develop2Android

+0

使用'0dp'來設置兩個'LinearLayouts'的高度,在這兩個'LinearLayouts'上設置了'weight'。 – Luksprog

回答

0

我終於得到了它的權利, 問題是,我已經設置了適配器在onCreate()方法, 解決此問題我已在mainFragment中的onCreateView()方法中設置適配器。

相關問題