BottomNavigationView导致图像加载延迟

时间:2019-03-04 07:25:41

标签: android imageview lag bottomnavigationview

0

在构建我的应用程序时,图像加载非常缓慢。当删除BottomNavigationView或工具栏后,它们似乎顺理成章地发生,应用程序运行平稳。安装程序是MainActivity,并且片段通过底部导航栏加载到其中。

public class FragmentActivity extends AppCompatActivity {

BottomNavigationView navi;
FragmentTransaction fragmentTransaction;


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 = new RewardsFragment();

        // 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();
    }



    navi = findViewById(R.id.navigation);


    Bundle intent = getIntent().getExtras();
    if(intent != null) {
            String venueID = intent.getString("venue_id");
            String venueName = intent.getString("venue_name");
            VenueFragment venueFragment = new VenueFragment();
            Bundle args = new Bundle();
            args.putString("venue_id", venueID);
            args.putString("venue_name", venueName);
            venueFragment.setArguments(args);
            switchFragment(venueFragment);

    } else {
        switchFragment(new SearchFragment());
    }

    unCheckAllMenuItems(navi.getMenu());
    navi.setOnNavigationItemSelectedListener(item -> {
        Fragment fragment;
        if (item.getItemId() == R.id.search) {
            item.setChecked(false);
            fragment = new SearchFragment();
            switchFragment(fragment);
            return false;

        } else if (item.getItemId() == R.id.rewards) {
            item.setChecked(false);
            fragment = new RewardsFragment();
            switchFragment(fragment);
            return false;
        }
        return false;

    });
}


private void switchFragment(Fragment fragment) {
    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

xml布局

<android.support.constraint.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<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="@color/white"
    app:itemBackground="@color/white"
    app:itemIconTint="@color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/navigation_bar_menu" />

  GridView gridview = getView().findViewById(R.id.postsGridTest);


        Log.v(TAG, "running");


        //declare adapter
        if (adapter != null) {
            adapter.setImages(image_list);
            //adapter.notifyDataSetChanged();
            waitingPosts = false; //reset this var now that we have loaded in more posts
            image_list = new ArrayList<String>();
            return;
        }

        adapter = new PostAdapter(getContext(), image_list);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gridview.setAdapter(adapter);
            }
        });

^用于将图像加载到gridview中的代码,尽管在将单个图像也加载到布局中的不同片段中时也会发生此问题

0 个答案:

没有答案