|
| 1 | +/* |
| 2 | + * Copyright 2019 Pranav Pandey |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.pranavpandey.android.dynamic.support.fragment; |
| 18 | + |
| 19 | +import android.os.Bundle; |
| 20 | +import android.view.LayoutInflater; |
| 21 | +import android.view.View; |
| 22 | +import android.view.ViewGroup; |
| 23 | + |
| 24 | +import androidx.annotation.NonNull; |
| 25 | +import androidx.annotation.Nullable; |
| 26 | +import androidx.fragment.app.Fragment; |
| 27 | +import androidx.viewpager.widget.ViewPager; |
| 28 | +import androidx.viewpager2.widget.ViewPager2; |
| 29 | + |
| 30 | +import com.google.android.material.tabs.TabLayout; |
| 31 | +import com.google.android.material.tabs.TabLayoutMediator; |
| 32 | +import com.pranavpandey.android.dynamic.support.R; |
| 33 | +import com.pranavpandey.android.dynamic.support.activity.DynamicActivity; |
| 34 | +import com.pranavpandey.android.dynamic.support.adapter.DynamicFragmentStateAdapter; |
| 35 | +import com.pranavpandey.android.dynamic.support.listener.DynamicViewPagerCallback; |
| 36 | + |
| 37 | +/** |
| 38 | + * An abstract {@link ViewPager} fragment to display multiple fragments inside the view pager |
| 39 | + * along with the tabs. |
| 40 | + * <p>Just extend this fragment and implement the necessary methods to use it inside an activity. |
| 41 | + */ |
| 42 | +public abstract class DynamicViewPager2Fragment extends |
| 43 | + DynamicFragment implements DynamicViewPagerCallback { |
| 44 | + |
| 45 | + /** |
| 46 | + * Fragment argument key to set the initial view pager page. |
| 47 | + */ |
| 48 | + public static String ADS_ARGS_VIEW_PAGER_PAGE = "ads_args_view_pager_page"; |
| 49 | + |
| 50 | + /** |
| 51 | + * View pager used by this fragment. |
| 52 | + */ |
| 53 | + private ViewPager2 mViewPager; |
| 54 | + |
| 55 | + /** |
| 56 | + * Tab layout used by this fragment. |
| 57 | + */ |
| 58 | + private TabLayout mTabLayout; |
| 59 | + |
| 60 | + /** |
| 61 | + * View pager adapter used by this fragment. |
| 62 | + */ |
| 63 | + private ViewPagerAdapter mAdapter; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 67 | + super.onCreate(savedInstanceState); |
| 68 | + |
| 69 | + setRetainInstance(false); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public View onCreateView(@NonNull LayoutInflater inflater, |
| 74 | + @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 75 | + return inflater.inflate(R.layout.ads_fragment_view_pager_2, container, false); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 80 | + super.onViewCreated(view, savedInstanceState); |
| 81 | + |
| 82 | + mViewPager = view.findViewById(R.id.ads_view_pager); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 87 | + super.onActivityCreated(savedInstanceState); |
| 88 | + |
| 89 | + ((DynamicActivity) getActivity()).addHeader(R.layout.ads_tabs, true); |
| 90 | + mTabLayout = getActivity().findViewById(R.id.ads_tab_layout); |
| 91 | + |
| 92 | + mAdapter = new ViewPagerAdapter(this, this); |
| 93 | + mViewPager.setOffscreenPageLimit(mAdapter.getItemCount()); |
| 94 | + mViewPager.setAdapter(mAdapter); |
| 95 | + |
| 96 | + new TabLayoutMediator(mTabLayout, mViewPager, |
| 97 | + new TabLayoutMediator.TabConfigurationStrategy() { |
| 98 | + @Override |
| 99 | + public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { |
| 100 | + tab.setText(getTitle(position)); |
| 101 | + } |
| 102 | + }).attach(); |
| 103 | + |
| 104 | + if (getArguments() != null && getArguments().containsKey(ADS_ARGS_VIEW_PAGER_PAGE)) { |
| 105 | + setPage(getArguments().getInt(ADS_ARGS_VIEW_PAGER_PAGE)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the tab layout used by this fragment. |
| 111 | + * |
| 112 | + * @return The tab layout used by this fragment. |
| 113 | + */ |
| 114 | + public TabLayout getTabLayout() { |
| 115 | + return mTabLayout; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the view pager used by this fragment. |
| 120 | + * |
| 121 | + * @return The view pager used by this fragment. |
| 122 | + */ |
| 123 | + public ViewPager2 getViewPager() { |
| 124 | + return mViewPager; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the currently selected view pager page or position. |
| 129 | + * |
| 130 | + * @return The currently selected view pager page or position. |
| 131 | + */ |
| 132 | + public int getCurrentPage() { |
| 133 | + return mViewPager.getCurrentItem(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Set the current page or position for the view pager. |
| 138 | + * |
| 139 | + * @param page The current position for the view pager. |
| 140 | + */ |
| 141 | + public void setPage(final int page) { |
| 142 | + mViewPager.setCurrentItem(page); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * View pager adapter to display the supplied fragments with tab titles. |
| 147 | + */ |
| 148 | + static class ViewPagerAdapter extends DynamicFragmentStateAdapter { |
| 149 | + |
| 150 | + private final DynamicViewPagerCallback dynamicViewPagerCallback; |
| 151 | + |
| 152 | + /** |
| 153 | + * Constructor to initialize an object of this class. |
| 154 | + * |
| 155 | + * @param fragment The fragment manager to get the child fragment manager. |
| 156 | + * @param dynamicViewPagerCallback The view pager callback to return the data. |
| 157 | + */ |
| 158 | + ViewPagerAdapter(@NonNull Fragment fragment, |
| 159 | + @NonNull DynamicViewPagerCallback dynamicViewPagerCallback) { |
| 160 | + super(fragment); |
| 161 | + |
| 162 | + this.dynamicViewPagerCallback = dynamicViewPagerCallback; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public @NonNull Fragment createFragment(int position) { |
| 167 | + return dynamicViewPagerCallback.createFragment(position); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public int getItemCount() { |
| 172 | + return dynamicViewPagerCallback.getItemCount(); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments