好的,所以我有一个Android应用程序,它为每个视图使用内置的NavigationDrawer Activity with Fragments。其中一个视图从Web服务中提取一些数据,但由于数据在一天内没有变化,我将值存储在首选项中以及打开选项卡的那一天的时间戳,以便Web服务方法不是每次用户打开该选项卡时都会调用。我遇到的问题是,当时间戳部分移动到更新视图的代码部分时,某些项目没有正确绘制(有些项目被压扁而其他项目没有显示正确的位置屏幕)。如果我单击刷新按钮来调用Web服务方法,然后以这种方式更新视图,它总是正确地绘制它们。我不知道我在哪里弄乱,但我确定这是我,因为Android对我来说很新鲜。
@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);