打开复杂视图时,我的导航抽屉滞后。这是我的MenuActivity代码:
public class MenuActivity extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout mDrawer;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// Set the menu icon instead of the launcher icon.
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Setup drawer view
findViewById(R.id.building_status_fragment).setOnClickListener(this);
findViewById(R.id.news_fragment).setOnClickListener(this);
findViewById(R.id.register_fragment).setOnClickListener(this);
findViewById(R.id.phone_view).setOnClickListener(this);
findViewById(R.id.services_fragment).setOnClickListener(this);
findViewById(R.id.contacs_fragment).setOnClickListener(this);
// first init
getFragmentManager().beginTransaction().replace(R.id.container, new BuildingStatusFragment()).commit();
setTitle(getString(R.string.title_building_progress)); // toolbar.setTitle dont work, hm
}
@Override
public void onClick(View v) {
mDrawer.closeDrawers();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
switch(v.getId()) {
case R.id.building_status_fragment:
fragmentTransaction.replace(R.id.container, new BuildingStatusFragment());
toolbar.setTitle(getString(R.string.title_building_progress));
break;
case R.id.news_fragment:
fragmentTransaction.replace(R.id.container, new NewsFeedFragment());
toolbar.setTitle(getString(R.string.title_news));
break;
case R.id.register_fragment:
fragmentTransaction.replace(R.id.container, new RegisterFragment());
toolbar.setTitle(getString(R.string.title_look));
break;
case R.id.services_fragment:
fragmentTransaction.replace(R.id.container, new ServicesFragment());
toolbar.setTitle(getString(R.string.title_services));
break;
case R.id.contacs_fragment:
fragmentTransaction.replace(R.id.container, new ContactsFragment());
toolbar.setTitle(getString(R.string.title_contacts));
break;
case R.id.phone_view:
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:74996782873")));
break;
}
fragmentTransaction.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// https://stackoverflow.com/questions/13721049/how-to-hide-default-keyboard-when-click-on-menu
if(getCurrentFocus() != null && getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
mDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我有"复杂"查看2张图片:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="@drawable/auto" />
<TextView
android:id="@+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:background="#B300A3DA"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="Покупка"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toptext"
android:layout_marginLeft="17dp"
android:layout_marginTop="4dp"
android:background="#B300A3DA"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="машиномест"
android:textColor="#fff"
android:textSize="18sp" />
<!--Костыль-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="15dp">
<!--Костыль-->
<LinearLayout
android:id="@+id/auto_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B3000000"
android:orientation="vertical"
android:paddingBottom="7dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Посмотреть"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="предложение"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="@drawable/rights" />
<TextView
android:id="@+id/toptext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:background="#B300A3DA"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="Оформление"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toptext2"
android:layout_marginLeft="17dp"
android:layout_marginTop="4dp"
android:background="#B300A3DA"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="прав собственности"
android:textColor="#fff"
android:textSize="18sp" />
<!--Костыль-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="15dp">
<!--Костыль-->
<LinearLayout
android:id="@+id/rights_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B3000000"
android:orientation="vertical"
android:paddingBottom="7dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Посмотреть"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="предложение"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
每次打开这个片段时它都会滞后。另外我注意到带有地图视图的片段也有一些滞后。我读了this post,看起来我应该在导航抽屉关闭后做我所有的工作(如果我在片段事务处理之前调用mDrawer.closeDrawers()并且它是相同的??),但是原因是什么如果我在初始化复杂视图的片段时打开抽屉,会滞后吗?这对我来说很奇怪。
同样调试者说,uithread需要2个工作并跳过帧。所以我应该优化我的&#34;复杂&#34;碎片或优化导航抽屉出现?如何? THX
答案 0 :(得分:2)
我看到你的代码和以下是我要做的更正
不要为相对布局设置背景颜色。它永远不会显示,因为Image会透支它。过度绘制会消耗处理能力。它会帮助你加快速度。
避免不必要的布局嵌套。线性内部线性,而不是尝试RelativeLayout或PercentRelativeLayout
尝试使用getWindow()。setBackgroundDrawable(null);因为您已将ScrollView背景设置为白色。
我希望这足以加速你的代码。
答案 1 :(得分:1)
我认为您正在使用导航抽屉标题背景图片,删除或更改导航抽屉标题背景图片(小尺寸),然后尝试一次