我是android开发的新手,我正在开发一个来自jfeinstein10的SlidingMenu库的应用程序,我在主屏幕上列出了一些最受好评的数据...... 现在我通过从SQLite获取数据并在片段活动中放置ListView来实现这一点
HomeFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
new ManageSQLiteResponse((ListView)rootView.findViewById(R.id.lv_top_rate)).execute();
return rootView;
}
ManagerSQLiteResponse电话:
public void updateList(ListView listView){
DietsListAdapter adapter = new DietsListAdapter(getActivity(), R.layout.single_top_rate, dietList);
listView.setAdapter(adapter);
}
每次我通过选择SlidingMenu来切换片段时,所有ListView都会再次填充,这会导致一些延迟,菜单将无法顺利打开或关闭...
那么,有没有办法运行AsyncTask.execute()并只填充一次ListView,而不是每次创建片段?通过这个我认为它将停止落后SlidingMenu
TVM
答案 0 :(得分:0)
使用此方法在容器布局中添加片段。
public void replaceFragment(Fragment fragment) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
Fragment fragmentPopped = manager.findFragmentByTag(backStateName) ;
if ( fragmentPopped != null )
manager.popBackStack(backStateName, 0);
else
ft.replace(R.id.container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
并在这样的代码中查找条件,如果
if ( adapter.getCount() > 0 )
{
// dont add listview.setadapter() method here.
}
elsse
{
// load data in listview.
}