BottomNavigationView cause lag with image loading

2020-04-18 07:13发布

问题:

0

When I'm building my app, the image loading is very laggy. It seems to happen in conjuction with adding a BottomNavigationView or Toolbar, when they are removed app runs smoothly. Setup is a MainActivity and fragments loaded into it thru bottom nav bar.

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 layout

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

^code for loading in an image into gridview, though the issue occurs when loading a single image into a layout as well, in a different fragment