慢速创建碎片 Android

huangapple go评论76阅读模式
英文:

slow creation of fragment android

问题

所以我有一个片段它的启动速度非常慢就像跳过了150多帧
我试图在片段内部设置ViewPager并添加一个onPageChangeListener在向某个方向滑动后禁用滑动

问题在于这真的降低了片段的加载速度我尝试将其移动到单独的线程和AsyncTask但仍然很慢

片段
```java

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) {
        // 填充此片段的布局
        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布局文件:

<!-- 在这里是你的 XML 布局内容 -->

适配器(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;
    }
}

这些是关于你的代码的翻译。如果你有其他问题,请随时提问。

英文:

So I have a fragment which launches really slowly like it skips 150+ frames.
I am trying to setup ViewPager inside of the fragment and add onPageChangeListener which disables swipe after swiping to a certain direction.

The problem is that it really slows down the fragment loading speed. I tried moving it to a separate thread and AsyncTask but its still slow.

Fragment:


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&lt;Void,Void,Void&gt;{

        @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;
        }
    }

}

The xml layout file for fragment:

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
                                                   xmlns:tools=&quot;http://schemas.android.com/tools&quot;
                                                   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
                                                   android:layout_height=&quot;match_parent&quot;
                                                   android:layout_width=&quot;match_parent&quot;
                                                   android:background=&quot;#fff&quot; android:animationCache=&quot;true&quot;
                                                   android:id=&quot;@+id/SettingsFragmentContainer&quot;
                                                   android:drawingCacheQuality=&quot;high&quot;&gt;


        &lt;androidx.appcompat.widget.Toolbar
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:background=&quot;?attr/colorPrimary&quot;
                android:theme=&quot;?attr/actionBarTheme&quot;
                android:minHeight=&quot;?attr/actionBarSize&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                android:id=&quot;@+id/SettingsActivityToolbar&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot;/&gt;

        &lt;TextView
                android:text=&quot;Settings&quot;
                android:layout_width=&quot;65dp&quot;
                android:layout_height=&quot;23dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot; android:layout_marginTop=&quot;18dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/Settings_textview&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot;/&gt;
        &lt;ImageButton
                android:layout_width=&quot;46dp&quot;
                android:layout_height=&quot;34dp&quot; app:srcCompat=&quot;@drawable/settings_icon_back&quot;
                android:id=&quot;@+id/backPressButton&quot;
                android:rotation=&quot;180&quot; android:backgroundTint=&quot;#fff&quot;
                android:background=&quot;@null&quot;
                app:layout_constraintStart_toStartOf=&quot;@+id/SettingsActivityToolbar&quot;
                android:layout_marginStart=&quot;2dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                android:layout_marginTop=&quot;16dp&quot; android:onClick=&quot;click&quot; android:drawingCacheQuality=&quot;high&quot;/&gt;
        &lt;TextView
                android:text=&quot;&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0.1dp&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/linebreak1&quot; android:layout_marginTop=&quot;10dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; android:background=&quot;#A1A8AC&quot;/&gt;
        &lt;Switch
                android:text=&quot;Test Test&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;47dp&quot; android:id=&quot;@+id/switch4&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                android:layout_marginStart=&quot;25dp&quot; android:layout_marginEnd=&quot;25dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:thumbTint=&quot;@drawable/switch_thumb_selector&quot;
                android:trackTint=&quot;@drawable/switch_track_selector&quot; android:layout_marginTop=&quot;18dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; tools:ignore=&quot;UseSwitchCompatOrMaterialXml&quot;/&gt;
        &lt;TextView
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0.1dp&quot; android:id=&quot;@+id/linebreak2&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                android:layout_marginStart=&quot;25dp&quot; android:layout_marginEnd=&quot;25dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:background=&quot;#A1A8AC&quot;
                android:layout_marginTop=&quot;10dp&quot; app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot;/&gt;
        &lt;TextView
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;67dp&quot; android:id=&quot;@+id/update&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot;
                app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                android:clickable=&quot;true&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintHorizontal_bias=&quot;0.0&quot;
                android:background=&quot;?attr/selectableItemBackground&quot; android:layout_marginTop=&quot;16dp&quot;
                app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot; android:onClick=&quot;onClickUpdate&quot;/&gt;
        &lt;TextView
                android:text=&quot;Current Version 1.0.1&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:clickable=&quot;true&quot;
                android:layout_height=&quot;0dp&quot; android:id=&quot;@+id/enter_version&quot;
                app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                android:layout_marginTop=&quot;36dp&quot; android:layout_marginEnd=&quot;206dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot;/&gt;
        &lt;TextView
                android:text=&quot;Check Update&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:clickable=&quot;true&quot;
                android:id=&quot;@+id/update_textview&quot;
                android:textAppearance=&quot;@style/ActionBar.nameText&quot; android:layout_marginEnd=&quot;228dp&quot;
                app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot; app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                android:layout_marginTop=&quot;10dp&quot;/&gt;
        &lt;com.testapp.test2.CustomViewPager
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;0dp&quot;
                app:layout_constraintStart_toStartOf=&quot;parent&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                android:id=&quot;@+id/ViewPager&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                app:layout_constraintBottom_toBottomOf=&quot;parent&quot;/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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

How do I fix this?

EDIT:

java.lang.NullPointerException: Attempt to invoke virtual method &#39;void androidx.fragment.app.Fragment.setMenuVisibility(boolean)&#39; 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)

答案1

得分: 0

我通过将构建类型从调试更改为发布来修复了它。

英文:

I fixed it by changing the build type from debug to release.

huangapple
  • 本文由 发表于 2020年10月15日 23:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64374555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定