所以我有一个片段,它启动得非常慢,就像跳过150多帧一样。我正在尝试在片段内部设置ViewPager,并添加onPageChangeListener,它在向某个方向滑动后禁用滑动。
问题是它确实降低了碎片加载的速度。我尝试将它移动到一个单独的线程和AsyncTask,但速度仍然很慢。
片段:
public class SettingsFragment extends Fragment {
public View mView;
public SettingsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Deprecated
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootview = inflater.inflate(R.layout.fragment_settings,container,false);
this.mView=rootview;
Task task=new Task();
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rootview;
}
@Deprecated
public class Task extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... voids) {
final CustomViewPager viewPager = mView.findViewById(R.id.ViewPager);
viewPager.setAdapter(new Adapter(getFragmentManager()));
viewPager.setCurrentItem(1);
viewPager.setOffscreenPageLimit(1);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
viewPager.setPagingEnabled(false);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return null;
}
}
}
片段的xml布局文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#fff" android:animationCache="true"
android:id="@+id/SettingsFragmentContainer"
android:drawingCacheQuality="high">
<androidx.appcompat.widget.Toolbar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/SettingsActivityToolbar" app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:text="Settings"
android:layout_width="65dp"
android:layout_height="23dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="18dp"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/Settings_textview"
android:textAppearance="@style/ActionBar.nameText"/>
<ImageButton
android:layout_width="46dp"
android:layout_height="34dp" app:srcCompat="@drawable/settings_icon_back"
android:id="@+id/backPressButton"
android:rotation="180" android:backgroundTint="#fff"
android:background="@null"
app:layout_constraintStart_toStartOf="@+id/SettingsActivityToolbar"
android:layout_marginStart="2dp" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp" android:onClick="click" android:drawingCacheQuality="high"/>
<TextView
android:text=""
android:layout_width="0dp"
android:layout_height="0.1dp" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/linebreak1" android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@+id/backPressButton" android:background="#A1A8AC"/>
<Switch
android:text="Test Test"
android:layout_width="0dp"
android:layout_height="47dp" android:id="@+id/switch4"
android:textAppearance="@style/ActionBar.nameText" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="25dp" android:layout_marginEnd="25dp"
app:layout_constraintEnd_toEndOf="parent" android:thumbTint="@drawable/switch_thumb_selector"
android:trackTint="@drawable/switch_track_selector" android:layout_marginTop="18dp"
app:layout_constraintTop_toBottomOf="@+id/backPressButton" tools:ignore="UseSwitchCompatOrMaterialXml"/>
<TextView
android:layout_width="0dp"
android:layout_height="0.1dp" android:id="@+id/linebreak2"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="25dp" android:layout_marginEnd="25dp"
app:layout_constraintEnd_toEndOf="parent" android:background="#A1A8AC"
android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@+id/switch4"/>
<TextView
android:layout_width="0dp"
android:layout_height="67dp" android:id="@+id/update"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:clickable="true"
android:textAppearance="@style/ActionBar.nameText" app:layout_constraintHorizontal_bias="0.0"
android:background="?attr/selectableItemBackground" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/switch4" android:onClick="onClickUpdate"/>
<TextView
android:text="Current Version 1.0.1"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="0dp" android:id="@+id/enter_version"
app:layout_constraintTop_toTopOf="@+id/update"
android:layout_marginTop="36dp" android:layout_marginEnd="206dp"
app:layout_constraintEnd_toEndOf="@+id/update"/>
<TextView
android:text="Check Update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/update_textview"
android:textAppearance="@style/ActionBar.nameText" android:layout_marginEnd="228dp"
app:layout_constraintEnd_toEndOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"
android:layout_marginTop="10dp"/>
<com.testapp.test2.CustomViewPager
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/ViewPager" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter.java
public class Adapter extends FragmentStatePagerAdapter {
public Adapter(FragmentManager fragmentManager){
super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull
@NotNull
@Override
public Fragment getItem(int position) {
if(position==0){
return new BlankFragment();
}
if(position==1){
return new SettingsFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
Logcat:
2020-10-15 07:51:34.234 14029-14034/com.testapp.test2 I/zygote64: Increasing code cache capacity to 1024KB
2020-10-15 07:51:35.302 14029-14029/com.testapp.test2 I/Choreographer: Skipped 165 frames! The application may be doing too much work on its main thread.
2020-10-15 07:51:50.250 14029-14034/com.testapp.test2 I/zygote64: Do full code cache collection, code=502KB, data=324KB
2020-10-15 07:51:50.252 14029-14034/com.testapp.test2 I/zygote64: After code cache collection, code=501KB, data=287KB
2020-10-15 07:51:51.006 14029-14034/com.testapp.test2 I/zygote64: Do partial code cache collection, code=503KB, data=297KB
2020-10-15 07:51:51.007 14029-14034/com.testapp.test2 I/zygote64: After code cache collection, code=503KB, data=297KB
2020-10-15 07:51:51.007 14029-14034/com.testapp.test2 I/zygote64: Increasing code cache capacity to 2MB
2020-10-15 07:51:51.886 14029-14034/com.testapp.test2 I/zygote64: Compiler allocated 8MB to compile void android.view.ViewRootImpl.performTraversals()
2020-10-15 07:51:54.868 14029-14029/com.testapp.test2 I/TouchMotionHandler: handler onDetached
我该如何解决这个问题?
编辑:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.fragment.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at androidx.fragment.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:189)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:22251)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:335)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:22251)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:105)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:247)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:898)
at android.view.View.measure(View.java:22251)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2500)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1564)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1826)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1446)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6954)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
at android.view.Choreographer.doCallbacks(Choreographer.java:737)
at android.view.Choreographer.doFrame(Choreographer.java:669)
2020-10-16 00:56:36.824 14663-14663/com.testapp.test2 E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:911)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
我通过将构建类型从调试更改为发布来修复它。