Android 加载片段UI时的奇怪行为
好的,我有一个Android应用程序,它使用内置的NavigationDrawer活动,每个视图都有片段。其中一个视图从web服务中提取一些数据,但由于数据在一天内不会更改,因此我将这些值存储在首选项中,并存储打开选项卡当天的时间戳,以便用户不会在每次打开该选项卡时调用web服务方法。但我遇到的问题是,当时间戳部分仅仅移动到更新视图的代码部分时,一些项目没有正确绘制(一些项目被压扁,而另一些项目没有在屏幕上显示正确的位置)。如果我单击“刷新”按钮调用web服务方法,然后更新视图,使其始终正确地绘制它们。我不知道我把事情搞砸了,但我肯定是我自己,因为安卓对我来说还很陌生Android 加载片段UI时的奇怪行为,android,android-fragments,Android,Android Fragments,好的,我有一个Android应用程序,它使用内置的NavigationDrawer活动,每个视图都有片段。其中一个视图从web服务中提取一些数据,但由于数据在一天内不会更改,因此我将这些值存储在首选项中,并存储打开选项卡当天的时间戳,以便用户不会在每次打开该选项卡时调用web服务方法。但我遇到的问题是,当时间戳部分仅仅移动到更新视图的代码部分时,一些项目没有正确绘制(一些项目被压扁,而另一些项目没有在屏幕上显示正确的位置)。如果我单击“刷新”按钮调用web服务方法,然后更新视图,使其始终正确地绘
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sales, container, false);
thisActivity = this.getActivity();
mtdHeaderTextView = (TextView)rootView.findViewById(R.id.mtdHeaderText);
mtdSeekBar = (SeekBar)rootView.findViewById(R.id.mtdSeekBar);
mtdTopSeekBar = (SeekBar)rootView.findViewById(R.id.mtdTopSeekBar);
mtdValueTextView = (TextView)rootView.findViewById(R.id.mtdValueTextView);
mtdGoalTextView = (TextView)rootView.findViewById(R.id.mtdGoalTextView);
mtdDateTextView = (TextView)rootView.findViewById(R.id.mtdDateTextView);
return rootView;
}
@Override
public void onResume() {
super.onResume();
CheckTimeStamp();
}
public static void CheckTimeStamp() {
SharedPreferences preferences = HelperClass.PreferenceFileName(thisActivity);
String timeStamp = preferences.getString(Constants.keySalesTimeStamp, "");
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String currentTime = HelperClass.GetSimpleDateFormat(date);
if (currentTime.equals(timeStamp)) {
foundMTDValue = preferences.getString(Constants.keyMTDValue, "");
foundYTDValue = preferences.getString(Constants.keyYTDValue, "");
UpdateMTDProgress();-->this is the method that adds values to the textviews and seekbars
}
else {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(Constants.keySalesTimeStamp, currentTime);
editor.apply();
GetMTDValue(); -->this calls the web service to get values and then calls UpdateMTDProgress like above
}
}
下面是UpdateMTDProgres部分的代码
public static void UpdateMTDProgress() {
//First update Header Text View
SharedPreferences preferences = HelperClass.PreferenceFileName(thisActivity);
int mtdGoal = preferences.getInt(Constants.keyMonthlyGoal, 0);
double percentage = Double.parseDouble(foundMTDValue) / mtdGoal;
NumberFormat format = NumberFormat.getPercentInstance();
String percentageString = format.format(percentage);
mtdHeaderTextView.setText("MTD Commissions (" + percentageString + ")");
//Get Current Date and Number of Days in Month
Calendar calendar = Calendar.getInstance();
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
int totalDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//Get Daily Goal for Rep based on mtdGoal and number of days
int dailyGoal = mtdGoal/totalDays;
//Get current day goal
int currentDayGoal = dailyGoal * currentDay;
//Check if current mtd value against currentdaygoal
if (Double.parseDouble(foundMTDValue) >= currentDayGoal) {
mtdSeekBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.seekbar_green));
}
else {
mtdSeekBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.seekbar_red));
}
//update progress seekbar values
mtdSeekBar.setMax(mtdGoal);
mtdSeekBar.setProgress((int) Double.parseDouble(foundMTDValue));
//update indicator seekbar values
mtdTopSeekBar.setMax(totalDays);
mtdTopSeekBar.setProgress(currentDay);
//Update Current Value and Goal TextViews
String currentValueString = HelperClass.ConvertStringToCurrency(foundMTDValue);
mtdValueTextView.setText(currentValueString);
String goalValueString = HelperClass.ConvertStringToCurrency(String.valueOf(mtdGoal));
mtdGoalTextView.setText(goalValueString);
Date date = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
String shortDate = dateFormat.format(date);
mtdDateTextView.setText(shortDate);
你能展示一下你的
UpdateMTDProgress()
方法吗?我从Update方法中添加了代码。只是一个建议,下次你发布代码时,你能让它更可读一点吗?比如,更好的格式?