gpt4 book ai didi

android - 如何避免滑动菜单滞后导致打开 fragment 卡顿?

转载 作者:行者123 更新时间:2023-11-30 01:23:13 29 4
gpt4 key购买 nike

在我的应用程序中,我在主要 Activity 中实现了滑动菜单抽屉。菜单的设计是自定义的,单击菜单中的列表项会打开一个 fragment 。

private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment=new LastTransactionsDetails();

break;
case 1:
fragment = new Statement_of_Transaction();
break;
case 2:
fragment = new AccountDetails();

break;
case 3:
fragment=new ProfileSetting();

break;
case 4:
fragment=new LastTransactionsDetails();

break;

default:
break;
}

if (fragment != null) {

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}

滑动菜单的关闭显示有一点延迟,导致菜单卡顿。 onCreateView fragment 正在调用一个异步任务,其中包含对 web 服务方法的调用。是不是因为 fragment 加载导致了延迟?如果是这样,那么我做了什么改变? fragment 代码如下:

@Override
public void onAttach(Context context) {
super.onAttach(context);

mActivity = context instanceof Activity ? (Activity) context : null;
viewUtils = new ViewUtils(mActivity);
webServicesParams = new WebServicesParams(mActivity);
database = new DbAdapter(context);
database.createDatabase();
database.open();
parseJsonResponse = new ParseJsonResponse();
showProgressDialog();

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.last_transactions_details, container, false);

listView_transactions = (ListView) rootView.findViewById(R.id.listView_transactions);

try {


String jsonResponse = webServicesParams.Last_Transaction();
arr_last_trans = parseJsonResponse.getTierDetails(jsonResponse);

adapter = new LastTransactionAdapter(arr_last_trans,getActivity());
listView_transactions .setAdapter(adapter);

dismissProgressDialog();
}

最佳答案

在单击导航项之后和加载 fragment 之前添加一些延迟,以顺利关闭幻灯片菜单。

private final Handler mDrawerHandler = new Handler();
//Wait for few millis before replacing fragment to allow slidemenu to fully close.
public void smoothReplaceFragment(final Fragment fragment) {
mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
replaceFragment(fragment);
}
}, 350);
}

//Replace Fragment
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}

关于android - 如何避免滑动菜单滞后导致打开 fragment 卡顿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com