Android从tablayout标签

时间:2018-04-10 05:02:57

标签: android android-layout android-studio tabs

我目前正在制作标签布局。几乎我已经做到了。

我的场景位于我的0位置我使用了自定义视图,其中包括imageview和textview。

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

1)在0索引片段我调用API。因此,如果没有数据,则显示没有可用数据,标签布局只显示一个图标。

2)如果用户想要从按钮点击添加数据,那么他可以填写数据,并且应该更改tablayout的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);
                }
            });

}

2 个答案:

答案 0 :(得分:2)

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 你可以找到自定义tab的textview然后设置textview为空

答案 1 :(得分:1)

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

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

和 Kotlin:

tabs.getTabAt(0)?.customView = null