Android从tablayout选项卡中删除自定义 View

标签 android android-layout tabs

我目前正在研究选项卡布局。几乎我已经做到了。

我的场景是在我的 0 位置,我使用了自定义 View ,其中包括 imageview 和 textview。

我在 0 位置的第一个标签有一些条件。他们是谁

1)在 0 索引 fragment 我正在调用 API。因此,如果没有数据,则不显示可用数据,并且选项卡布局仅显示一个图标。

2)如果用户想通过按钮单击添加数据,那么从新的 Activity 中他可以填充数据,并且应该更改标签布局 0 位置的图标,并且将显示带有选定日期的 TextView 。

问题 :
如果我在 onResume() 方法中调用 setUpTabIcons() 方法,则会重复 imageview 图标。


这是屏幕截图。

enter image description here

这是我的代码。

 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);
                }
            });

}

最佳答案

为了从选项卡布局中删除自定义 View ,您只需将该选项卡的自定义 View 设置为 null。这是Java中的示例:

tabs.getTabAt(0).setCustomView(null);
和 Kotlin :
tabs.getTabAt(0)?.customView = null

关于Android从tablayout选项卡中删除自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745663/

相关文章:

Android动态改变布局

android - ViewPager 中的 onTouchListener

android - onConfigurationChanged 屏幕停止在设备上旋转。在模拟器上工作

android - ActionBar 颜色不变

android - 如何动态清除和设置ScrollView的内容?

java - 从最近的菜单启动应用程序不会启动主要 Activity

javascript - 将 .click() 添加到动态创建的选项卡

css - 重新创建错误折叠的内容 IE

javascript - jQuery 选项卡 : Disable window location hash

android - 在 android 移动设备中访问本地主机时出现 404 错误,但在 pc 浏览器上工作