/*
 * Copyright (C) 2016 Jorge Ruesga
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ruesga.rview.fragments;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruesga.rview.BaseActivity;
import com.ruesga.rview.R;
import com.ruesga.rview.databinding.ViewPagerBinding;

public abstract class PaginableFragment extends Fragment {

    private class PageFragmentAdapter extends FragmentPagerAdapter {

        public PageFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return getFragment(position);
        }

        @Override
        public int getCount() {
            return getTabs().length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getTabs()[position];
        }
    }

    private ViewPagerBinding mBinding;

    public abstract String[] getTabs();

    public abstract Fragment getFragment(int position);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_pager, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBinding.viewPager.setOffscreenPageLimit(getTabs().length + 1);
        mBinding.viewPager.setAdapter(new PageFragmentAdapter(getFragmentManager()));
        ((BaseActivity) getActivity()).configureTabs(mBinding.viewPager);
    }
}

@@ -2382,16 +2382,21 @@
 pter(get
+Child
 Fragment
@@ -2487,11 +2487,130 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public final void onDestroyView() %7B%0A        super.onDestroyView();%0A        mBinding.unbind();%0A    %7D
 %0A%7D%0A

Use the proper fragment manager