2013-03-09 53 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