Android Studio - java - in onBindViewHolder, Line breaks and white space being added to RecyclerView

47 Views Asked by At

Description: I'm encountering an issue in my Android app. I have a List of text document titles. Clicking on one item in the list opens the text document in a RecyclerView - the document is loaded within a TextView. The user can then swipe the TextView left to load the next item (the next text document) or right to see the previous item (the previous text document) within the List. NOTE: I have not implemented the swipe logic in code at this point in my app development. What I have found is that the RecyclerView adds white space/line breaks as follows, using a List with 15 items:

  • If I click on the first item in the list, the text document opens without any extra white space at the top of the RecyclerView - this is what I desire - however there are ~14 line breaks at the end of the item - extra white space not in the text document
  • If I click on an item from the middle of the list, say item #8, there would be 7 line breaks above the text document and 7 extra line breaks added to the end
  • If I click on the last item in the list, there are approximately 14 line breaks added to the top of the document however there are no extra line breaks added to the end of the text
  • Performing test with 15 item List's however if I create a new List with a single item (a single text document) this document opens perfectly and there is not any extra space above or below the file text
  • For my list of 15 items, clicking on the first item has not any white space on top and all the white space at the bottom. For my list of 15 items, clicking on the last item in the list, all the white space is on top while the bottom contains no white space, text ends and the scrolling stops appropriately.

Using Log statements: As I scroll down the list, the onBindViewHolder() method gets called multiple times and if I rem out the "clearing" code:

//holder.bind("");

additional line breaks seem to be added to the items. Also on scrolling back up, the extra line breaks persist, and there now appears multiple iterations of the same text document item with increasing amounts of white space. So without the holder.bind(""); code the RecyclerView is dynamically adding iterations of the text document as I scroll up and down the page.

I've tried the following approaches to address the issue, but none have worked:

  • Ensured that the logic inside onBindViewHolder() is efficient and doesn't perform unnecessary operations.

  • Verified that my ViewHolder implementation properly recycles views and updates their content with the new data

  • Checked for data consistency and properly notified the adapter of any changes using notifyDataSetChanged().

  • In onBindViewHolder moved my conditional logic so I reset the TextView for every item`

  • I have cleaned project and rebuilt project but no impact on the problem

Here's my onBindViewHolder() method:

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d("SteList", "(SongLyricsAdapter) - line #83 - onBindViewHolder called");
        Songs song = dbHelper.getSongDetailsById((int) songsList.get(position).getSongId());
        if (position == selectedSongPosition) {
            // Load the lyrics of the selected song
            String songURI = song.getSongURI();
            String songFileName = song.getSongFileName();
            String lyrics = loadSongLyrics(context, songURI, songFileName);
            
            // Reset TextView state
            holder.lyricsTextView.setText(""); // Clear previous text
            holder.lyricsTextView.setScrollY(0); // Scroll to the top

            // Trim and set the lyrics
            String trimmedLyrics = lyrics.trim();
            holder.bind(trimmedLyrics);

        } else {
            // Clear the lyrics for other positions
            holder.bind("");
        }
    }

Here is the xml that inflates and displays the text:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyricsTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Song Content"
    android:textSize="16sp"
    android:padding="16dp" />

I'd appreciate any insights or suggestions on how to resolve this issue. I might need to provide more code - please suggest what else might be helpful.

Thank you!

1

There are 1 best solutions below

0
kiteandwindsurfer On BEST ANSWER

I was able to "fix" or perhaps better "work around" this by sending a different object to the RecyclerView adapter. While having the problem with the extra white space I was sending an array of Songs objects and even though I was filtering for only the Songs object that the user clicked, the RecyclerView added extra lines into the view thus ticking the songs in the List even though only one should be loaded at a time. To get around this I am now sending only a single song at a time and this is working fine.

Here is my updated onBindViewHolder method:

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        // Fetch the details of the selected song using the songId
        Songs song = dbHelper.getSongDetailsById(songId);

        // Load the lyrics of the selected song
        String songURI = song.getSongURI();
        String songFileName = song.getSongFileName();
        String lyrics = loadSongLyrics(context, songURI, songFileName);

        // Reset TextView state
        holder.lyricsTextView.setText(""); // Clear previous text
        holder.lyricsTextView.setScrollY(0); // Scroll to the top

        // Trim and set the lyrics
        String trimmedLyrics = lyrics.trim();
        holder.lyricsTextView.setText(trimmedLyrics);

    }

So I am still using a RecyclerView and ticking through the Songs, however I feel I am still missing something ... or perhaps not. I'll leave this here as this is what got me back to coding and completing this section of the app.

Thanks for reading and good luck to you!