简体   繁体   中英

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

explains that CardFragment can easily add a card by:

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?

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. CardFragment is just a convenience for a standard wearable layout.

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.

On getFragment(int row, int col) method from your FragmentGridPagerAdapter class, you can create as many fragments as you want.

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. Also as matiash pointed you can use any fragment that you like in such structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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