2013-03-09 56 views
0

我正在學習在android上創建應用程序,我有一個片段功能的問題。Android的片段視圖不能​​選擇佈局

我使用eclipse創建了一個新的黑色活動,並選擇了「滑動視圖+標題欄」導航類型。

我跑它和它的完全工作顯示第1,第2,第3節 我想要做的就是選擇不同的佈局,每個部分,所以我調整了代碼是這樣的:

package fr.mpsn.networkclient; 

import android.R.layout; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.NavUtils; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class HomeActivity extends FragmentActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_home, menu); 
     return true; 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Log.i("info", ""+position); 
      Fragment fragment = new DummySectionFragment("view_publishmessage"); 
      switch (position) { 
      case 0: 
       fragment =null; 
       fragment = new DummySectionFragment("view_publishmessage"); 
      case 1: 
       fragment =null; 
       fragment = new DummySectionFragment("view_timeline"); 
      case 2: 
       fragment =null; 
       fragment = new DummySectionFragment("view_profile"); 
      } 

      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
      case 0: 
       return "Nouvelle publication".toUpperCase(); 
      case 1: 
       return "Timeline".toUpperCase(); 
      case 2: 
       return "Profil".toUpperCase(); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 
     public final String fragmentLayoutName; 

     public DummySectionFragment(String fragmentLayoutName) { 
      this.fragmentLayoutName = fragmentLayoutName; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // Create a new TextView and set its text to the fragment's section 
      // number argument value. 

      return inflater.inflate(
        this.getResources().getIdentifier(this.fragmentLayoutName, 
          "layout", "fr.mpsn.networkclient"), null); 
     } 
    } 

} 

問題是,所有不同的部分使用視圖配置文件佈局,即使案件被正確解析...

你有什麼想法,我該如何改進這個代碼,使其工作更好?

回答

1

難道你不想錯過switch中的break聲明嗎? 沒有它,代碼繼續到下一個案例:

switch(cond) { 
case A: 
    print("hello!"); 
    //break; 
case B: 
    print("hello again!"); 
    //break; 
} 
=> 
hello! 
hello again! 

而且,最好是使用空構造的片段,否則重新創建片段時(對於配置更改後你就會有問題例)。
Do fragments really need an empty constructor?

您可以使用Fragment.setArguments()Fragment.getArguments()通過您fragmentLayoutName

+0

哈哈真可惜......謝謝尼科皮! – 2013-03-09 17:51:11

+0

我創建了一個更好的代碼的新版本,我會盡快回復(低信譽限制)作爲答案,希望它能幫助人們解決這個問題 – 2013-03-09 17:53:36

+0

如果nicopico解決了您的問題,您應該接受他的回答。另外,在新的ADT(21.1)中,除了空構造函數之外的任何東西都會導致lint錯誤,並且代碼不會編譯。 – Matt 2013-03-09 18:22:56