在构建应用程序时,在应用程序中添加底部导航栏会导致严重的滞后问题,有人知道为什么会这样吗?我已经尝试过BottomNavigationView,Toolbar甚至带有Image按钮的简单LinearLayout。安装程序是MainActivity,并且片段通过底部导航栏加载到其中。测试应用程序时,存在滞后问题,当从布局中删除导航栏时,这些问题都会消失。有谁知道为什么会这样吗?注意,将BottomNavigationView与以下switch语句一起使用会使情况变得更糟
public class FragmentActivity extends AppCompatActivity {
Fragment currentFragment;
Fragment selectedFragment;
public static final String TAG = FragmentActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
SearchFragment searchFragment = new SearchFragment();
currentFragment = searchFragment;
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, currentFragment).commit();
setBottomNavBar();
}
}
public void setBottomNavBar() {
Log.d(TAG, "setting up bar");
LinearLayout bottomBar = findViewById(R.id.toolbar);
ImageButton search = bottomBar.findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new SearchFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
ImageButton user = bottomBar.findViewById(R.id.user);
user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new MyUserFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
ImageButton trending = bottomBar.findViewById(R.id.trending);
trending.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), TrendingMapTest.class); //creating new intent, passing in the UserFeedActivity calling it
startActivity(intent);
}
});
ImageButton upload = bottomBar.findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CameraAndGalleryActivity.class); //creating new intent, passing in the UserFeedActivity calling it
startActivity(intent);
}
});
ImageButton rewards = bottomBar.findViewById(R.id.rewards);
rewards.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new RewardsFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
}
}
这是主要活动xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:gravity="bottom">
<ImageButton
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="false"
android:background="@android:color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/search_icon" />
<ImageButton
android:id="@+id/user"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/person_icon" />
<ImageButton
android:id="@+id/trending"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/forknife" />
<ImageButton
android:id="@+id/upload"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="false"
android:background="@android:color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/upload_icon" />
<ImageButton
android:id="@+id/rewards"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="false"
android:background="@android:color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/present_icon" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
尝试
activity_main.xml
$("#table1").dataTable({
columns: [
{ data: "account name"}
{ data: "balance", className: "text-right" }
]
});
menu / navigation_menu.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/bottamNav"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorAccent"
app:menu="@menu/navigation_menu" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home"
android:title="@string/title_shop"
/>
<item
android:id="@+id/navigation_watch"
android:icon="@drawable/ic_shopping_cart"
android:title="@string/title_offer" />
<item
android:id="@+id/navigation_download"
android:icon="@drawable/ic_wallet"
android:title="@string/title_order" />
</menu>
更新
转到您的Menifest文件中,在“活动”中只需将HardwareAccelerated为true,以防止打开抽屉滞后
public class MainActivity extends AppCompatActivity {
BottomNavigationView navi;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navi = findViewById(R.id.navigation);
switchFragment(new HomeFragment());
navi.setOnNavigationItemSelectedListener(item -> {
Fragment fragment;
if (item.getItemId() == R.id.navigation_home) {
fragment = new HomeFragment();
switchFragment(fragment);
return true;
} else if (item.getItemId() == R.id.navigation_watch) {
fragment = new WatchLaterFragment();
switchFragment(fragment);
return true;
} else if (item.getItemId() == R.id.navigation_download) {
fragment = new DownloadsFragment();
switchFragment(fragment);
return true;
}
return false;
});
}
private void switchFragment(Fragment fragment) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}