嗨,大家好,我試圖設置一個進度條,而在片段轉換時,我的第一個片段onClick調用第二個,它會顯示之前做一些繁重的工作。 這個繁重的工作不使用asynctask,因爲它是緩存中的html的解析。Android進度條,而片段轉換
什麼是最好的方法使這項工作?
我的第一個片段:
private void onClick(final MoodleCourseContent[] courseContent,
View layout, final int contentIdx) {
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String courseId = Integer.toString(v.getId());
String courseName = courseContent[contentIdx].getName();
String topicId = (String) v.getTag();
Bundle bundle = new Bundle();
bundle.putString("courseId", courseId);
bundle.putString("courseName", courseName);
bundle.putString("topicId", topicId);
FragmentManager fragmentManager = getActivity()
.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
FragTopics insideTopicsFrag = new FragTopics();
insideTopicsFrag.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction
.replace(R.id.mainFragment, insideTopicsFrag);
fragmentTransaction.commit();
}
});
}....
我的第二個片段:
public class FragTopics extends Fragment {
Object course;
// ManSession Manager Class
ManSession session;
String courseId;
Long topicId;
String courseName;
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
public FragTopics() {
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
session = new ManSession(getActivity().getApplicationContext());
courseId = getArguments().getString("courseId");
topicId = Long.parseLong(getArguments().getString("topicId"));
courseName = getArguments().getString("courseName");
MoodleCourseContent[] courseTopics = new ManContents(getActivity()
.getApplicationContext()).getContent(courseId);
MoodleCourseContent singleTopic = new ManContents(getActivity()
.getApplicationContext()).getTopic(topicId, courseTopics);
return createTopics(singleTopic, courseName, courseId, topicId);
}......
如果你有很多繁重的計算,最好的方法可能是在一個異步的話或者在另一個線程中做。否則,在計算完成之前,您將無法渲染片段,然後您將無法顯示進度條。 – Catherine
是的,我假設這是唯一的方法去我避免,因爲我必須改變很多東西,但如果沒有其他方式,我將不得不這樣做 – firetrap