社区,
我正在为我的学校开发一个应用程序,我最近添加了一个新功能,这让我有问题。这是一个你可以添加我们自己的课程安排的功能(不知道在englisch中叫什么,因为我来自德国)。所以在那个布局中有很多Spinners,必须进行初始化......我猜这就是为什么我的应用程序滞后,当viewPager创建这个Fragment时...这是我的代码片段:
vf = (ViewFlipper) getActivity().findViewById(R.id.vf);
DaySpin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
vf.setDisplayedChild(arg2);
if(arg2==0) {
for(int i=0;i<12;i++) {
String monSpinnerID = "MonSpin" + i;
String monSubSpinnerID = "MonSubSpin" + i;
int monResID = getResources().getIdentifier(monSpinnerID, "id", "de.ebs_hannover.sean.elsaapp");
int monSubResID = getResources().getIdentifier(monSubSpinnerID, "id", "de.ebs_hannover.sean.elsaapp");
MonSpin[i] = (Spinner) getActivity().findViewById(monResID);
MonSubSpin[i] = (Spinner) getActivity().findViewById(monSubResID);
MonSpin[i].setAdapter(teacherAdapter);
MonSpin[i].setSelection(getState(monSpinnerID));
MonSubSpin[i].setSelection(getState(monSubSpinnerID));
final int j = i;
MonSpin[i].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
saveState(arg2,"MonSpin" + j);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
MonSubSpin[i].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
saveState(arg2,"MonSubSpin" + j);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
} else if (arg2==1) {
for(int i=0;i<12;i++) {
String tueSpinnerID = "TueSpin" + i;
String tueSubSpinnerID = "TueSubSpin" + i;
int tueResID = getResources().getIdentifier(tueSpinnerID, "id", "de.ebs_hannover.sean.elsaapp");
int tueSubResID = getResources().getIdentifier(tueSubSpinnerID, "id", "de.ebs_hannover.sean.elsaapp");
TueSpin[i] = (Spinner) getActivity().findViewById(tueResID);
TueSubSpin[i] = (Spinner) getActivity().findViewById(tueSubResID);
TueSpin[i].setAdapter(teacherAdapter);
TueSpin[i].setSelection(getState(tueSpinnerID));
TueSubSpin[i].setSelection(getState(tueSubSpinnerID));
final int j = i;
TueSpin[i].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
saveState(arg2,"TueSpin" + j);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
TueSubSpin[i].setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
saveState(arg2,"TueSubSpin" + j);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
[...]
DaySpin变量是包含所有工作日的Spinner
MonSpin是Spinner-Array,它包含了星期一所有12节课的所有教师
MonSubSpin是Spinner-Array,包含星期一所有12节课的所有科目
并且我已经通过仅初始化显示的旋转器来优化它
例如:如果DaySpin项目&#34;星期一&#34;如果选中,则会显示星期一的布局,并且仅显示星期一&#34;正在初始化。
是的,我需要为每个工作日的每个课程制作一个额外的Spinner,因为它需要在关闭并重新打开应用程序后保存和加载。
我的想法是只在片段真正显示时才初始化那些Spinners而不仅仅是Created。这与我为AsyncTask做的一样......就像在这段代码中一样剪断:
[...]
mRunnable = new Runnable() {
@Override
public void run() {
if(check2==0) {
mHandler.postDelayed(mRunnable, 1000);
check2=1;
if(check==0) {
check2=0;
} else {
new backgroundTeacherLoading().execute();
}
}
}
};
if(check3==0) {
check2=0;
mRunnable.run();
check3=1;
}
[...]
如果显示片段LessonsFragment,则检查等于1,如果显示任何其他片段,则等于0 ...但我不知道如何为我的初始化做同样的事情...
或者也许是我在&#34; onActivityCreated()&#34;中的所有教师的初始化(总计= 87)。必须要做的方法,以便适配器初始化旋转器的所有教师?这是一个剪辑:
[...]
teacherCountTotal = getTeacherTotal();
if(!getFirstRun()) {
if(teacherCountTotal == 0) {
resetRunned();
} else {
teachers = new String[teacherCountTotal+1];
for(int i=0;i<teacherCountTotal+1;i++) {
teachers[i] = readFile("Teacher" + i);
}
}
}
[...]
那么你是否有人知道如何减少我的片段的滞后,这是LessonsFragment的旁边?