Kent Hawkings
Kent Hawkings

Reputation: 2793

Android: AdapterView lags when dynamically adding view to LinearLayout

I'm currently creating a music player application. My music library activity is a ViewPager with four pages/fragments (corresponding to albums, artists, all songs and Playlists). Three of the fragments are just GridView's and the last one is a ListView (the all songs fragment). When the application is first started everything runs perfectly smoothly. However, when the user selects a song to play (for the first time) a LinearLayout (called the 'now playing bar') is dynamically created at the bottom of the screen with the information about the currently playing song. At this points all of my GridView's and my ListView begin lagging quite badly when scrolled. Weirdly enough my ViewPager doesn't seem to experience any lag as switching between Fragment's is as seamless as before. This problem persists when the user exits the player and then resumes it (in which case the 'now playing bar' is already visible).

There is also another rather confusing factor in all this. When the user selects an album, artist or playlist from the library this creates a new activity which is just a ListView containing all the songs from said artist/album/playlist. The 'now playing bar' is also added to this activity but it causes no slowdown whatsoever. This leads me to conclude that the problem is related to the ViewPager and/or Fragment's in my library activity.

I have been completely stumped by this and would greatly appreciate any help.

Here is the XML layout for my library activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"    
android:orientation="vertical"
android:id="@id/library_view"
>
<include layout="@layout/sd_error"/>
<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0" 
    />
<include layout="@layout/now_playing_bar"/>

And here is the code used to create the 'now playing bar':

public static void updateNowPlaying(Activity a) 
{
    View nowPlayingView = a.findViewById(R.id.now_playing_bar);

    if (nowPlayingView == null)         
        return;

    try 
    {                                
        if (true && MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) 
        {
            ImageView cover = (ImageView) nowPlayingView.findViewById(R.id.cover);
            TextView title = (TextView) nowPlayingView.findViewById(R.id.title);
            TextView artist = (TextView) nowPlayingView.findViewById(R.id.artist);

            title.setText(MusicUtils.sService.getTrackName());
            String artistName = MusicUtils.sService.getArtistName();                

            if (MediaStore.UNKNOWN_STRING.equals(artistName)) 
                artistName = a.getString(R.string.unknown_artist_name);

            artist.setText(artistName);

            cover.setImageURI(ContentUris.withAppendedId(
                 sArtworkUri, sService.getAlbumId()));

            if (cover.getDrawable() == null)
                cover.setImageResource(R.drawable.no_album_cover);                  

            nowPlayingView.setVisibility(View.VISIBLE);
            nowPlayingView.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    Context c = v.getContext();
                    c.startActivity(new Intent(c, PlayerActivity.class));
                }
            });
            return;
        }
    } 
    catch (RemoteException ex) {}
    nowPlayingView.setVisibility(View.GONE);
}

Upvotes: 2

Views: 945

Answers (1)

Kent Hawkings
Kent Hawkings

Reputation: 2793

OK, so it turns out that the reason for the lag was because of I had android:layout_height="match_parent" and android:layout_weight="1.0". Apparently if you are setting the weight as such you should use android:layout_height="0dp" for better performance. I have absolutely no idea why this is the case but it fixed the problem.

Upvotes: 1

Related Questions