2013-04-13 173 views
0

我目前在使用ViewPager替換某個片段時遇到了問題。片段id想要替換的是我的「Departments」,它有一個Imagebutton id喜歡用來開始替換。我試圖應用來自其他類似問題的一些建議(其中大部分是舊的,並且在允許嵌套片段的新api發佈之前)並且沒有成功。使用嵌套片段會更容易嗎?我是Android應用開發新手,所以任何幫助都會很棒。提前致謝。在viewpager中替換片段

這裏是我的FragmentAcitivty

public class ViewPagerStyle extends FragmentActivity { 
    private ViewPager mViewPager; 
    private ViewPagerAdapter adapter; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setUpView(); 
     setTab(); 
    } 
    private void setUpView(){  
    mViewPager = (ViewPager) findViewById(R.id.viewPager); 
    adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager()); 
    mViewPager.setAdapter(adapter); 
    mViewPager.setCurrentItem(0); 
    } 
    private void setTab(){ 
      mViewPager.setOnPageChangeListener(new OnPageChangeListener(){ 

         @Override 
         public void onPageScrollStateChanged(int position) {} 
         @Override 
         public void onPageScrolled(int arg0, float arg1, int arg2) {} 
         @Override 
         public void onPageSelected(int position) { 
          // TODO Auto-generated method stub 
          switch(position){ 
          case 0: 
           findViewById(R.id.first_tab).setVisibility(View.VISIBLE); 
           findViewById(R.id.second_tab).setVisibility(View.INVISIBLE); 
           findViewById(R.id.third_tab).setVisibility(View.INVISIBLE); 
           break; 

          case 1: 
           findViewById(R.id.first_tab).setVisibility(View.INVISIBLE); 
           findViewById(R.id.second_tab).setVisibility(View.VISIBLE); 
           findViewById(R.id.third_tab).setVisibility(View.INVISIBLE); 
           break; 

          case 2: 
           findViewById(R.id.first_tab).setVisibility(View.INVISIBLE); 
           findViewById(R.id.second_tab).setVisibility(View.INVISIBLE); 
           findViewById(R.id.third_tab).setVisibility(View.VISIBLE); 
           break; 
          } 
         } 

        }); 

    } 
} 

FragmentPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter { 

    private Context _context; 

    public ViewPagerAdapter(Context context, FragmentManager fm) { 
     super(fm); 

      _context = context; 
     } 
    @Override 
    public Fragment getItem(int position) { 
     Fragment f = new Fragment(); 
     switch(position){ 
     case 0: 
      f=PubView.newInstance(_context);  
      break; 
     case 1: 
      f=MyView.newInstance(_context); 
      break; 
     case 2: 
      f=Departments.newInstance(_context); 
      break; 

     } 

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

} 

部門片段與按鈕

public class Departments extends Fragment { 
    public static Fragment newInstance(Context context) { 
     Departments f = new Departments(); 

     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View root = (View) inflater.inflate(R.layout.activity_departments, null); 

     ImageButton engineeringButton = (ImageButton)root.findViewById(R.id.engineeringButton); 
     engineeringButton.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v){ 
       Fragment newFragment = Engineering.newInstance(null); 
       FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
       transaction.addToBackStack(null); 
       transaction.replace(R.id.viewPager, newFragment).commit(); 

      } 
     }); 

     return root; 
    } 

} 

而且,這裏是我的main.xml文件,它承載viewpager

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/purple" 
    android:orientation="vertical" > 

    <TableLayout 
     style="@style/layout_f_w" 
     android:stretchColumns="*" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      style="@style/layout_wrap" 
      android:background="@color/white" > 

      <!-- First Tab --> 

      <LinearLayout 
       android:id="@+id/first_text" 
       style="@style/layout_f_w" 
       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/textView1" 
        style="@style/text_title" 
        android:text="pubView" 
        android:textColor="@color/purple" /> 
      </LinearLayout> 

      <!-- Second Tab --> 

      <LinearLayout 
       android:id="@+id/second_text" 
       style="@style/layout_f_w" 
       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/textView1" 
        style="@style/text_title" 
        android:gravity="center" 
        android:text="myView" 
        android:textColor="@color/purple" /> 
      </LinearLayout> 

      <!-- Third Tab --> 

      <LinearLayout 
       android:id="@+id/third_text" 
       style="@style/layout_f_w" 
       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/textView1" 
        style="@style/text_title" 
        android:text="Dept." 
        android:textColor="@color/purple" /> 
      </LinearLayout> 
     </TableRow> 
    </TableLayout> 
    <!-- Include Tab Indicator --> 

    <include 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/indicator" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="fill_parent" 
     android:layout_height="450dp" /> 

    <ImageButton 
     android:id="@+id/settingsButton" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:layout_marginLeft="50dp" 
     android:background="@drawable/settings_button" 
     android:src="@drawable/settings_button" /> 

</LinearLayout> 

我對此感到困惑的一件事是,我不知道將哪個id放入transaction.replace(r.id.viewPager,newfragment)的第一個參數中......我讀了它需要集裝箱但是當我使用這個我收到的logcat運行時錯誤:

04-13 21:41:48.680: E/AndroidRuntime(960): java.lang.IllegalArgumentException: No view found for id 0x7f0a0029 (com.pvcalendar:id/viewPager) for fragment Engineering{4089a730 #0 id=0x7f0a0029} 

回答

0

我假設你想要的Engineering片段是一個完全新的一頁,因爲你是不是在你ViewPagerAdapter使用它。如果是這種情況,請在佈局中創建一個新的Activity,並使用Engineering片段,然後從engineeringButton點擊啓動Activity

問題是你試圖將你的工程片段推入到R.layout.activity_departments的View層次結構中,並且在那裏有(有希望)沒有ViewPager,因此是錯誤。

+1

是的,你是對的。事實證明,這是一個簡單的解決方案。我原本沒有爲我的Departments佈局設置「id」,這就是爲什麼片段管理器無法找到替換任何內容的視圖。只要我給我的activity_departments。xml一個ID可以在transaction.replace()中識別它現在可以工作。骨頭錯過了我,但我真的很感謝你的意見,並且你想幫助我。謝謝:) – playboy

1

我認爲重點是使用片段作爲容器。

在你ViewPagerAdapter:

@Override 
    public Fragment getItem(int position) { 
     /* 
     * IMPORTANT: This is the point. We create a RootFragment acting as 
     * a container for other fragments 
     */ 
     if (position == 0) 
      return new RootFragment(); 
     else 
      return new StaticFragment(); 
    } 

RootFragment版面應該像:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/root_frame" > 
</FrameLayout> 

,並直接,你與你的第一個 「真正」 的片段填充:

public class RootFragment extends Fragment { 

private static final String TAG = "RootFragment"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    /* Inflate the layout for this fragment */ 
    View view = inflater.inflate(R.layout.root_fragment, container, false); 

    FragmentTransaction transaction = getFragmentManager() 
      .beginTransaction(); 
    /* 
    * When this container fragment is created, we fill it with our first 
    * "real" fragment 
    */ 
    transaction.replace(R.id.root_frame, new FirstFragment()); 

    transaction.commit(); 

    return view; 
} 
} 

最後,你可以替換碎片。例如,你的「真實」的片段裏,你可以有一個按鈕:

btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      FragmentTransaction trans = getFragmentManager() 
        .beginTransaction(); 
      /* 
      * IMPORTANT: We use the "root frame" defined in 
      * "root_fragment.xml" as the reference to replace fragment 
      */ 
      trans.replace(R.id.root_frame, new SecondFragment()); 

      /* 
      * IMPORTANT: The following lines allow us to add the fragment 
      * to the stack and return to it later, by pressing back 
      */ 
      trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
      trans.addToBackStack(null); 

      trans.commit(); 
     } 
    }); 

我已經開發了一個示例應用程序,顯示了類似的概念。您可以替換ViewPager中的片段而不更改爲新的活動。該代碼是可用的:

https://github.com/danilao/fragments-viewpager-example

+0

哎呀,對不起......這個......更正了! – dlao

+0

酷 - 謝謝:-)請修復你的其他答案。 – kleopatra

+0

嗨@dlao嗨@dlao我試過你的項目它真的很好的解決方案,但我遇到了一個問題,當我通過單擊該片段中的按鈕來訪問第二個fragment1,現在如果我滑動並轉到下一個片段(靜態)..如果按下回到它關閉第二個片段...所以知道如果我刷到firstfragment insted secondfragment出現第一個片段..你有任何解決方案? – Pramod