简体   繁体   English

FragmentGridPagerAdapter内部的自定义布局

[英]Custom layout inside FragmentGridPagerAdapter

I am trying to add a custom layout page within cards. 我正在尝试在卡片中添加自定义布局页面。

Here: https://developer.android.com/training/wearables/ui/2d-picker.html 此处: https//developer.android.com/training/wearables/ui/2d-picker.html

explains that CardFragment can easily add a card by: 解释了CardFragment可以通过以下方式轻松添加卡:

CardFragment fragment = CardFragment.create(title, text, page.iconRes);

now if I decided I want to have a custom layout, how do I add it instead of creating a CardFragment? 现在,如果我决定要使用自定义布局,该如何添加它而不是创建CardFragment?

Check this image: 检查此图像:

在此处输入图片说明

The third page is a full screen custom layout. 第三页是全屏自定义布局。 How can I achieve this? 我该如何实现?

FragmentGridPagerAdapter can actually support any subclass of Fragment that you need to use. FragmentGridPagerAdapter实际上可以支持您需要使用的Fragment任何子类。 CardFragment is just a convenience for a standard wearable layout. CardFragment只是标准可穿戴布局的一种便利。

So, you can just create a custom Fragment with a simple layout (such as a full-size ImageView ) and return it for the appropriate page index. 因此,您可以使用简单的布局(例如全尺寸ImageView )创建自定义Fragment并将其返回以用于适当的页面索引。

On getFragment(int row, int col) method from your FragmentGridPagerAdapter class, you can create as many fragments as you want. 在FragmentGridPagerAdapter类的getFragment(int row,int col)方法上,您可以创建任意数量的片段。

you just need to test your row/col values in order to instantiate your fragment class in the correct position. 您只需要测试您的行/列值即可将片段类实例化到正确的位置。

In your case, something like this: 您的情况是这样的:

public Fragment getFragment(int row, int col) {

    Fragment fragment;
    if (row == 0 && col == 2) {
        fragment = new YourFullScreenFragment();
    } else {
        fragment = CardFragment.create(title, text, page.iconRes);
    }

    return fragment;

}

Cheers 干杯

CardFrame is probably that one that you are looking for - it very similar to CardFragment but you can implement your own layout to it. CardFrame可能就是您要寻找的那个-它与CardFragment非常相似,但是您可以实现自己的布局。 Also as matiash pointed you can use any fragment that you like in such structure. 同样,正如matiash指出的那样,您可以在这种结构中使用任何您喜欢的片段。

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

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