简体   繁体   English

片段选项卡和具有不同的布局

[英]Fragment Tabs and with different layouts

So it seems that the standard for tabs in android now, is to use a tabhost in a FragmentActivity and then have each tab as Fragment. 因此,现在看来,Android中选项卡的标准是在FragmentActivity中使用tabhost,然后将每个选项卡作为Fragment。 The issue im having here is, it seems that the whole system of having side-side fragments for same FragmentActivity(tablets or landscape mode) or having one Fragment launch another FragmentActivity with the second Fragment(if its a small device or on rotation) wont work, because you cant have FragmentActivities for each tab. 此处的问题是,似乎具有相同FragmentActivity(平板电脑或横向模式)的侧面碎片或具有一个Fragment的整个系统将与第二个Fragment一起启动另一个FragmentActivity(如果它是小型设备或旋转)不会工作,因为您不能为每个选项卡都具有FragmentActivities。 So how do i implement the main advantage point of having Fragments on tabs? 那么我该如何实现在选项卡上使用Fragments的主要优势呢?

regards, 问候,

Yes, you can have fragmentActivities for each tab. 是的,每个选项卡都可以具有fragmentActivities。

This is a example for PagerAdapter: 这是PagerAdapter的示例:

public class ExamplePagerAdapter extends FragmentPagerAdapter {

    public ExamplePagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }


    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = null;
        switch(position){
        case 0:
        {
        f = new ArrayListFragment();
        // set arguments here, if required
        Bundle args = new Bundle();
        f.setArguments(args);
        break;
        }
        case 1:
        {
            f = new PrincipalFragment();
            // set arguments here, if required
            Bundle args = new Bundle();
            f.setArguments(args);
            break;
        }
        case 2:
        {   
            f = new ViewFragment();
            // set arguments here, if required
            Bundle args = new Bundle();
            f.setArguments(args);
            break;
        }   
        default:
          throw new IllegalArgumentException("not this many fragments: " + position);
        }


        return f;


}

} }

You can use this to call every activity for each fragment. 您可以使用它来调用每个片段的每个活动。

public class PrincipalFragment extends Fragment {

    private int mNum;
    private EditText et;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.scrollview, container, false);



        ImageView i = (ImageView)v.findViewById(R.id.imageView1);
          ImageView i2 = (ImageView)v.findViewById(R.id.imageView2);

          ImageView i4 = (ImageView)v.findViewById(R.id.imageView4);
          ImageView i5 = (ImageView)v.findViewById(R.id.imageView5);


          ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
          ImageDownloader imageDownloader = new ImageDownloader();
          imageDownloader.download(URLnames.URLS[1], i);
          imageDownloader.download(URLnames.URLS[2], i2);
          imageDownloader.download(URLnames.URLS[3], i4);
          imageDownloader.download(URLnames.URLS[4], i5);


        return v;

Like the example above, I call the layout scrollview, so when you gonna create scrollview.xml you need to specify the details. 像上面的例子一样,我将布局称为scrollview,因此当您创建scrollview.xml时,需要指定详细信息。

New > android xml file > Select Layout type > Next > Choose a specific configuration ( you can use by density, by orientation and etc... ) > Finish. 新建> android xml文件>选择布局类型>下一步>选择特定配置(您可以按密度,方向等使用…)>完成。

Now eclipse will gonna create a different folder for each specific configuration and will manage it own by own when you run different screen sizes or sdkversion. 现在,eclipse将为每个特定的配置创建一个不同的文件夹,并且当您运行不同的屏幕尺寸或sdkversion时,将自己管理它。

Read Supporting Multiple Screens for more information. 阅读支持多屏支持以获取更多信息。

Don´t know how to implements tabscroll properly? 不知道如何正确实现Tabscroll? Visit this answer: Actionbarsherlock + tabs + multi fragments? 访问此答案: Actionbarsherlock +选项卡+多个片段?

Inside the fragment activity you can create a case to select screen-orientation or put it in AndroidManifest.xml. 在片段活动中,您可以创建一个案例以选择屏幕方向,或将其放入AndroidManifest.xml中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM