Android Fragment View无法选择布局

时间:2013-03-09 17:31:46

标签: android android-fragmentactivity

我正在学习在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 个答案:

答案 0 :(得分:1)

您是否遗漏了break中的某些switch声明? 没有它,代码继续下一个案例:

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