1

在我的Android 應用。但它不顯示內容。 我FragmentActivity是的Android viewpager不顯示我使用<strong>viewpager</strong>和<strong>FragmentActivity</strong>佈局

public class DropPickup extends FragmentActivity { 

private DPAdapter myAdapter; 
private ViewPager myPager; 



@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     myAdapter = new DPAdapter(getSupportFragmentManager()); 

     myPager = (ViewPager) findViewById(R.id.dp_viewpager); 
     myPager.setAdapter(myAdapter); 
    } 


public static class MyAdapter extends FragmentPagerAdapter { 
     public MyAdapter(FragmentManager fm) { 
      super(fm); 
     } 

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

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
      case 0: 
       return new PickUpFragment(); 
      case 1: 
       return new DropFragment(); 

      default: 
       return null; 
      } 
     } 
    } 

}

和我的網頁適配器如下。

public DPAdapter(FragmentManager fm) { 
    super(fm); 
    // TODO Auto-generated constructor stub 



} 

@Override 
public Fragment getItem(int position) { 
    // TODO Auto-generated method stub 

    switch (position) { 
    case 0: 
     return new PickUpFragment(); 

    case 1: 
     return new DropFragment(); 
    default: 
     return null; 
    } 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 2; 
} 

}

這是我的一個片段

public class DropFragment extends Fragment { 

TableLayout DTbl; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View viewx=inflater.inflate(R.layout.fragment_drop, container, false); 
    DTbl = (TableLayout) viewx.findViewById(R.id.dpp_tbl_prs_tbl); 



    //return super.onCreateView(inflater, container, savedInstanceState); 
    return viewx; 
} 

}

這是我的佈局來看尋呼機

<android.support.v4.view.ViewPager 
       android:id="@+id/dp_viewpager" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" /> 

所以有人可以幫我圖出來的公關oblem。謝謝 !!!!

回答

0

什麼是寫兩個獨立的類MyAdapter & DPAdapter時候都一樣的,所以,把它們的一個目的。否則你的代碼看起來很好。

更新

使用此代碼,並保持MyAdapter類

myAdapter = new MyAdapter(getSupportFragmentManager()); 

     myPager = (ViewPager) findViewById(R.id.dp_viewpager); 
     myPager.setAdapter(myAdapter); 

這裏是我的代碼,我如何使用viewpager

public class Book extends FragmentActivity { 
    private static final String[] CONTENT = new String[] { "A", "B", "C"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.book); 


     FragmentStatePagerAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager()); 

     ViewPager pager = (ViewPager)findViewById(R.id.pager); 
     pager.setAdapter(adapter); 

     pager.setCurrentItem(CONTENT.length); 
    } 

    class MyFragmentPageAdapter extends FragmentStatePagerAdapter { 
     public MyFragmentPageAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 

if(CONTENT[position].equals("A")) 
{ 
return Page1.newInstance(CONTENT[position]); 
} else{ 
       return Page2.newInstance(CONTENT[position]); 
      } 

     @Override 
     public int getCount() { 
      return CONTENT.length; 
     } 
    } 
} 

,其中第一頁和第二頁是片段

+0

它是我的FragmentActivity的問題嗎? –