我目前正在开发选项卡布局。几乎已经完成了。
我的场景是在我位置为0的地方,我使用了包括ImageView和TextView的自定义视图。
我在0位置的第一个选项卡有一些条件。它们是:
1)在索引为0的片段中,我调用API。所以如果没有数据,则显示“无可用数据”,并且选项卡布局仅显示一个图标。
2)之后,如果用户想要从按钮单击添加数据,则可以从新的acttiviy中填充数据,并且tablayout的0位置上的图标应更改,并将TextView显示为选择的日期。
问题: 如果我在onResume()方法中调用setUpTabIcons()方法,则ImageView图标会重复。
以下是截图。
以下是我的代码。
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);
}
});
}