Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

android - 为什么一个简单的片段需要很长时间才能加载(Why a simple fragment takes a long time to load)

I'm using a bottom navigation view with fragments, the fragment switching is handled in my MainActivity like so:

(我正在使用带有片段的底部导航视图,片段切换在MainActivity中进行如下处理:)

 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;

        switch (item.getItemId()) {
            case R.id.navigation_income:
                fragment = income;
                break;
            case R.id.navigation_budget:
                fragment = budget;
                break;
            case R.id.navigation_expenses:
                fragment = expenses;
                break;
            case R.id.navigation_tools:
                fragment = tools;
                break;
            case R.id.navigation_savings:
                fragment = savings;
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
        return true;
    }
};

Where inciome/expenses/budget etc is an instance of the fragment created when MainActivity loads to avoid the fragments being recreated each time, they are just created once, stored and then switched to.

(当inciome / expenses / budget等是MainActivity加载时为避免每次都重新创建片段而创建的片段的一个实例,它们仅创建一次,存储然后切换到。)

This worked fine while I was just getting the layout sorted but now that I'm actually trying to create the fragments its all gone downhill.

(当我只是对布局进行排序时,这种方法效果很好,但是现在我实际上是在尝试创建所有下坡的片段。)

This is my IncomeFragment layout:

(这是我的IncomeFragment布局:)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="350dp"
    android:adjustViewBounds="false"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@drawable/income_top" />

The image loaded in the imageView is tiny (11kb hdpi -> 113kb xxxhdpi) so I don't see why that would be the issue but this page takes a good 3-4 seconds to load.

(imageView中加载的图像很小(11kb hdpi-> 113kb xxxhdpi),因此我看不到为什么会出现问题,但是此页面需要花费3-4秒的时间才能加载。)

As for the code behind this fragment is basically completely empty, this is my IncomeFragment code:

(至于这个片段背后的代码基本上是完全空的,这是我的IncomeFragment代码:)

public class IncomeFragment extends Fragment {

private IncomeViewModel incomeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    incomeViewModel =
            ViewModelProviders.of(this).get(IncomeViewModel.class);
    View root = inflater.inflate(R.layout.fragment_income, container, false);
    return root;
      }
}

the viewmodel is empty.

(viewmodel为空。)

Why is such a simple view taking such a ridiculously long time to load?

(为什么这么简单的视图需要这么长的时间才能加载?)

  ask by Coffee translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...