Tuesday, 22 May 2018

Viewpager With tabbar

public class HomeViewPagerFragment extends Fragment {


    private Activity activity;
    private NonSwipeableViewPager viewPager;
    public static TabLayout tabLayoutBottom;

    public static Handler chageTitleHandler;
    public static Handler homeFragmentSet;
    public static CustomTextView txtBottomBadgeCount;
    public static FrameLayout llBadgeBottom;

    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_viewpager_fragment, container, false);

        activity = getActivity();

        findViews(rootView);

        onClickListner();

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        setCustomTabView();

        ((MainActivity) activity).changeTitle("");

        changeTitle(0);


        chageTitleHandler = new Handler(new Handler.Callback() {
            @Override            public boolean handleMessage(Message msg) {

                changeTitle(tabLayoutBottom.getSelectedTabPosition());
                return false;
            }
        });


        homeFragmentSet = new Handler(new Handler.Callback() {
            @Override            public boolean handleMessage(Message msg) {

                tabLayoutBottom.getTabAt(0).select();

                return false;
            }
        });

        if (AppSharedPrefersec.getInstance(activity).getBooleanData("isLogin")) {

            getNotificationUnreadCount();
        }

        return rootView;

    }

    private void getNotificationUnreadCount() {

        CommonUtils.getWsseToken(activity);

        ApiClient.getAPI(AppConstant.URL_NOTIFICATION_COUNT, new Callback<ResponseBody>() {
            @Override            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                int code = response.code();

                String url = response.raw().request().url().toString();

                Log.e("Status Code", code + "");

                Log.e("Count Url", url + "");

                if (code == HttpsURLConnection.HTTP_OK) {

                    try {

                        String result = response.body().string();

                        Log.e("Url", result + "");

                        JSONObject jsonObject = new JSONObject(result.toString());

                        NotificationCount = jsonObject.getString("total");


                        if (!NotificationCount.isEmpty()) {

                            setCustomTabView();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


            }

            @Override            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }

    private void onClickListner() {


    }


    private void findViews(View rootView) {

        viewPager = rootView.findViewById(R.id.viewPagerHome);
        tabLayoutBottom = (TabLayout) rootView.findViewById(R.id.tabBottomHomeViewpagerFragment);

        viewPager.setOffscreenPageLimit(2);


    }


    TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());

            changeTitle(tab.getPosition());


        }

        @Override        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override        public void onTabReselected(TabLayout.Tab tab) {

        }
    };


    private void changeTitle(int currentItem) {

        switch (currentItem) {

            case 0:

                ((MainActivity) activity).changeTitle("");


                break;
            case 1:

                ((MainActivity) activity).changeTitle(getString(R.string.salons));


                break;
            case 2:

                ((MainActivity) activity).changeTitle(getString(R.string.timeline));


                break;
            case 3:

                ((MainActivity) activity).changeTitle(getString(R.string.more));


                break;

        }

    }


    private void setCustomTabView() {

        tabLayoutBottom.removeAllTabs();

        tabLayoutBottom.addTab(tabLayoutBottom.newTab().setCustomView(prepareTabView(0)));

        tabLayoutBottom.addTab(tabLayoutBottom.newTab().setCustomView(prepareTabView(1)));

        tabLayoutBottom.addTab(tabLayoutBottom.newTab().setCustomView(prepareTabView(2)));

        tabLayoutBottom.addTab(tabLayoutBottom.newTab().setCustomView(prepareTabView(3)));

        tabLayoutBottom.setTabGravity(TabLayout.GRAVITY_FILL);

        tabLayoutBottom.setOnTabSelectedListener(onTabSelectedListener);

      /*  tabLayoutBottom.post(new Runnable() {            @Override            public void run() {                tabLayoutBottom.setupWithViewPager(viewPager);            }        });*/    }


    private View prepareTabView(int i) {

        View view = getLayoutInflater().inflate(R.layout.custom_tab_home, null);

        ImageView imgTab = (ImageView) view.findViewById(R.id.iv_tabImage);

        llBadgeBottom = (FrameLayout) view.findViewById(R.id.llBadgeTabBarHome);

        txtBottomBadgeCount = (CustomTextView) view.findViewById(R.id.txtBadgeTabBarHome);

        switch (i) {

            case 0:
                imgTab.setImageResource(R.drawable.selector_tab_home);
                llBadgeBottom.setVisibility(View.GONE);
                break;
            case 1:
                imgTab.setImageResource(R.drawable.selector_tab_salon_service);
                llBadgeBottom.setVisibility(View.GONE);
                break;
            case 2:
                imgTab.setImageResource(R.drawable.selector_tab_time_line);
                llBadgeBottom.setVisibility(View.GONE);
                break;
            case 3:
                imgTab.setImageResource(R.drawable.selector_tab_more);
                if (!NotificationCount.isEmpty()) {

                    int not_count = Integer.parseInt(NotificationCount);

                    if (not_count > 0) {

                        txtBottomBadgeCount.setText(NotificationCount);
                        llBadgeBottom.setVisibility(View.VISIBLE);

                    } else {
                        llBadgeBottom.setVisibility(View.GONE);
                    }
                } else {
                    llBadgeBottom.setVisibility(View.GONE);
                }
                break;

        }


        return view;
    }


    class MyAdapter extends FragmentPagerAdapter {

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

        /**         * Return fragment with respect to Position .         */
        @Override        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new HomeFragment();
                case 1:
                    return new SalonServiceFragment();
                case 2:
                    return new TimeLineFragment();
                case 3:
                    return new MoreFragment();
            }
            return null;
        }

        @Override        public int getCount() {

            return 4;

        }

        /**         * This method returns the title of the tab according to the position.         */
        @Override        public CharSequence getPageTitle(int position) {

            switch (position) {
                case 0:
                    String MAP = "";
                    return MAP;
                case 1:
                    String NEARBY = "";
                    return NEARBY;
                case 2:
                    String FAVORITE = "";
                    return FAVORITE;
                case 3:
                    String Fs = "";
                    return Fs;
            }
            return null;
        }
    }

    @Override    public void onResume() {
        super.onResume();

        changeTitle(tabLayoutBottom.getSelectedTabPosition());


        if (tabLayoutBottom.getSelectedTabPosition() == 2) {


            //TimeLineFragment.volumeUpVideoHandler.sendMessage(new Message());
        } else {
            //TimeLineFragment.volumeDownVideoHandler.sendMessage(new Message());        }

    }

}

XML

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">


    <com.tpb.CustomViews.NonSwipeableViewPager        android:id="@+id/viewPagerHome"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"/>



    <android.support.design.widget.TabLayout        android:id="@+id/tabBottomHomeViewpagerFragment"        android:layout_width="match_parent"        android:layout_height="?attr/actionBarSize"        android:background="@color/colorPrimary"        app:tabIndicatorColor="@null" />

</LinearLayout>

No comments:

Post a Comment