简体   繁体   中英

Android remove custom view from tablayout tab

I am currently working on tab layout. Almost i have done it.

My scenario is at my 0 position i have used custom view which includes imageview and textview.

My first tab at 0 position have some conditions. Which they are

1) At 0 index fragment i am calling API. so if there is no data then display no data available and tab layout simply shows a icon.

2) After if user wants to add data from button click then from new acttiviy he can fill the data and icon at 0 position of tablayout should be changed and textview will be displayed with selected date.

Problem : If i call setUpTabIcons() method in onResume() method then imageview icons are repeated.

Here is screenshot.

在此处输入图片说明

Here is my code.

 private void setupTabIcons() {

    if (mediaPrefs.getString(Constant.SharedPreferences_wedding_id, "").length() > 0) {
        List<EventData> eventData = appDatabase.eventListDao().getSelectedWeddingEvents(Integer.parseInt(mediaPrefs.getString(Constant.SharedPreferences_wedding_id, "0")));


        View view = LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab_event_date_view, null);
        TextView txt_day = (TextView) view.findViewById(R.id.txt_day);
        TextView txt_month = (TextView) view.findViewById(R.id.txt_month);
        ImageView img_event = (ImageView)view.findViewById(R.id.img_icon_events);

        if (eventData != null && eventData.size() > 0) {

            if (eventData.get(0).getEventdate().length() > 0) {

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

                DateFormat day_format = new SimpleDateFormat("dd");
                DateFormat month_format = new SimpleDateFormat("MMM");
                Date date_day = null, date_month = null;
                try {
                    date_day = simpleDateFormat.parse(eventData.get(0).getEventdate());
                    date_month = simpleDateFormat.parse(eventData.get(0).getEventdate());

                    String day = day_format.format(date_day);
                    String month = month_format.format(date_month);

                    txt_day.setText(day);
                    txt_month.setText(month);
                    img_event.setVisibility(View.GONE);
                    txt_day.setVisibility(View.VISIBLE);
                    txt_month.setVisibility(View.VISIBLE);

                    Log.v("asfasf","heree2");


                    tabs.getTabAt(0).setCustomView(view);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {

                Log.v("asfasf","heree1");
                img_event.setVisibility(View.VISIBLE);
                txt_day.setVisibility(View.GONE);
                txt_month.setVisibility(View.GONE);

                img_event.setImageResource(R.mipmap.host_add_event_small);

                tabs.getTabAt(0).setCustomView(view);

            }

        } else {

            Log.v("asfasf","heree");

            img_event.setVisibility(View.VISIBLE);
            txt_day.setVisibility(View.GONE);
            txt_month.setVisibility(View.GONE);
            img_event.setImageResource(R.mipmap.host_add_event_small);

            tabs.getTabAt(0).setCustomView(view);
        }
    }
    tabs.getTabAt(1).setIcon(R.mipmap.camera_icon);
    tabs.getTabAt(2).setIcon(R.mipmap.chat_icon);
    tabs.getTabAt(3).setIcon(R.mipmap.notification_icon);
    tabs.getTabAt(4).setIcon(R.mipmap.chat_userprofile_light);

    tabs.setRotationX(180);
    LinearLayout tabListed = ((LinearLayout) tabs.getChildAt(0));
    for (int position = 0; position < tabListed.getChildCount(); position++) {
        LinearLayout item = ((LinearLayout) tabListed.getChildAt(position));
        item.setRotationX(180);
    }
    tabs.addOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(view_pager) {

                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    super.onTabSelected(tab);
                    if (tab.getPosition() == 0) {

                        View tabView = tab.getCustomView();

                        TextView txt_day = (TextView) tabView.findViewById(R.id.txt_day);
                        TextView txt_month = (TextView) tabView.findViewById(R.id.txt_month);


                        if(txt_day.getText().toString().trim().length()>0 && txt_month.getText().toString().trim().length()>0){
                            int tabIconColor = ContextCompat.getColor(getActivity(), R.color.dark_gray_wedding);
                            txt_day.setTextColor(tabIconColor);
                            txt_month.setTextColor(tabIconColor);
                        }else{
                            ImageView img_event = (ImageView)tabView.findViewById(R.id.img_icon_events);
                            int tabIconColor = ContextCompat.getColor(getActivity(), R.color.dark_gray_wedding);
                            if(img_event!=null){
                                img_event.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                            }
                        }

                    } else {
                        int tabIconColor = ContextCompat.getColor(getActivity(), R.color.dark_gray_wedding);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                    }
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    super.onTabUnselected(tab);
                    if (tab.getPosition() == 0) {

                        TextView txt_day = (TextView) tab.getCustomView().findViewById(R.id.txt_day);
                        TextView txt_month = (TextView) tab.getCustomView().findViewById(R.id.txt_month);

                        if(txt_day.getText().toString().trim().length()>0 && txt_month.getText().toString().trim().length()>0){
                            int tabIconColor = ContextCompat.getColor(getActivity(), R.color.light_gray_wedding);
                            txt_day.setTextColor(tabIconColor);
                            txt_month.setTextColor(tabIconColor);
                        }else{
                            ImageView img_event = (ImageView)tab.getCustomView().findViewById(R.id.img_icon_events);
                            int tabIconColor = ContextCompat.getColor(getActivity(), R.color.light_gray_wedding);
                            if(img_event!=null){
                                img_event.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                            }

                        }

                    } else {
                        int tabIconColor = ContextCompat.getColor(getActivity(), R.color.light_gray_wedding);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                    }
                }

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

}
TextView txt_day =(TextView)tabs.getTabAt(0).getCustomView().findViewById(R.id.txt_day);
TextView txt_month =(TextView)tabs.getTabAt(0).getCustomView().findViewById(R.id.txt_month);
txt_day.setText("");
txt_month.setText("");

@Piyush You can find the textview of the custom tab then set textview blank

In order to remove a custom view from a tab layout you just need to set the custom view for that tab to null. Here's an example in Java:

tabs.getTabAt(0).setCustomView(null);

and Kotlin:

tabs.getTabAt(0)?.customView = null

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