0

我正在尝试使用页面标题条,我需要在我的一个片段中放置三个搜索栏。这是我的代码:

public class MainActivity 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_main);

    // 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_main, 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.
        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (position) {
        case 0:
            fragment = new Page1();
            args.putInt(Page1.ARG_SECTION_NUMBER, position + 1);
            break;
        case 1:
            fragment = new Page2();
            args.putInt(Page2.ARG_SECTION_NUMBER, position + 1);
            break;
        case 2:
            fragment = new Page3();
            args.putInt(Page3.ARG_SECTION_NUMBER, position + 1);
            break;
        }
        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 getString(R.string.title_section1).toUpperCase();
        case 1:
            return getString(R.string.title_section2).toUpperCase();
        case 2:
            return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

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

    public Page1() {
    }

    @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.
        // TextView textView = new TextView(getActivity());
        // textView.setGravity(Gravity.CENTER);
        // textView.setText(Integer.toString(getArguments().getInt(
        // ARG_SECTION_NUMBER)));
        SeekBar sb1 = new SeekBar(getActivity());
        SeekBar sb2 = new SeekBar(getActivity());
        SeekBar sb3 = new SeekBar(getActivity());
        return sb1;
    }
}

public static class Page2 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public Page2() {
    }

    @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.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.LEFT);
        textView.setText("Second tab");
        return textView;
    }
}

public static class Page3 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public Page3() {
    }

    @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.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.RIGHT);
        textView.setText("third tab");
        return textView;
    }
}
}

这实际上有效,但我在第一页只有一个搜索栏。何我可以放他们三个???谢谢!!!!

4

1 回答 1

1

在这段代码上:

    SeekBar sb1 = new SeekBar(getActivity());
    SeekBar sb2 = new SeekBar(getActivity());
    SeekBar sb3 = new SeekBar(getActivity());
    return sb1;

您正在创建 3 个 SeekBars 但只返回第一个。在返回语句之后,其他两个正在被快速收集。在该onCreateView方法中,您必须创建一个 GroupView,该组将具有 3 个 SeekBar 或您需要的任何布局。最好/最简单的方法是创建一个 XML 布局并使用LayoutInflater在函数上传递的。类似于:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag_page1, null);
}
于 2012-12-26T18:41:16.053 回答