【问题标题】:Manage tab activity with fragments from another classes使用来自其他类的片段管理选项卡活动
【发布时间】:2012-12-29 11:02:40
【问题描述】:

我在另一个类中有一个片段,我想从我的标签活动(Google 直接提供)中启动它:

这是我的 FragmentActivity 中控制片段的部分,我不想要虚拟的,而是我在另一个类中创建的那个

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.

                    // Here is my fragment
        Fragment fragment = new MyFragment();
        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;
    }
}

这是我的 MyFragment :

public class MyFragment extends Fragment{

public Search() {
    // TODO Auto-generated constructor stub
}
public View onCreateView(String name, Context context, AttributeSet attrs) {
    // TODO Auto-generated method stub
    TextView textView = new TextView(context);
    textView.setText("search page");
    return textView;
}
}

但它不起作用。

【问题讨论】:

    标签: java android class android-fragments


    【解决方案1】:

    好的,答案很简单:

    public View onCreateView(String name, Context context, AttributeSet attrs) {
    // TODO Auto-generated method stub
    TextView textView = new TextView(context);
    textView.setText("search page");
    return textView;
    }
    

    不是真正的方法,我什至不知道为什么在我创建类时 Eclipse 给了我这个。

    真正的方法是:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        TextView textView = new TextView(context);
        textView.setText("search page");
        return textView;
    }
    

    这只是参数的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      相关资源
      最近更新 更多