我已经实现了从 web 下载数据然后在两个ActionBar.Tab
s 中显示它的应用程序。我有一个问题。如果我从一个选项卡切换到另一个选项卡,应用程序会开始另一次下载,并且在下载未完成时,应用程序会冻结。如果互联网连接速度很慢,它会变得非常烦人。我决定添加ProgressDialog
到应用程序以向用户显示应用程序正在从网络下载数据。我添加了一段实现下载的代码ProgressDialog
,AsyncTask
但这没有帮助。我理解为什么会发生这种情况,但找不到解决方法:(
将适合选项卡的数据表示为Fragment
类的实例。这个实例创建后会被添加到transaction
中,只有添加mFragment
对象到 中后transaction
,app 才会切换到另一个选项卡。
这是tabListener
代码的一部分:
// ...
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
if (mFragment == null) {
/*
* Creation of ParkFragment is the reason why app locks!! Because in
* ParkFragment data is being downloaded from web
*/
mFragment = new ParkFragment(mUrl, mActivity);
/*
* mFragment can't be added to transaction until downloading is
* finished, that's why app doesn't switch fast
*/
transaction.add(android.R.id.content, mFragment, mTag);
} else {
transaction.attach(mFragment);
}
}
请,如果有人有任何想法如何实施ProgressDialog
以避免标签之间切换的延迟,请与它分享。
感谢您的阅读。
更新:我已经阅读了这个问题的答案,但不明白该实施将如何帮助我管理延迟:Changing Tabs is Slow/Laggy - Using Fragments。
升级版:
public class ParkFragment extends ListFragment {
private ArrayList<Cinemas> cinema;
private CinemasAdapter cinemaAdapter;
private String url;
private Activity activity;
public ParkFragment (String cinema,Activity activ){
url = cinema;
activity = activ;
}
public void onCreate(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
cinema = new Handler().handle(url,activity);
cinemaAdapter = new CinemasAdapter(activity, R.layout.movie_data_row, cinema);
setListAdapter(cinemaAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cinemas movie = cinemaAdapter.getItem(position);
Intent intent = new Intent (activity, More.class);
intent.putExtra("Cinemas", movie);
intent.putExtra("data", movie.getBitmap());
Bundle translateBundle =
ActivityOptions.makeCustomAnimation(activity,
R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
startActivity (intent, translateBundle);
}
}