Description:
我在Android应用程序中遇到了一个问题.我有List
个文本文档标题. 点击list
中的一个item
打开RecyclerView
中的文本文档—文档被加载到TextView
中. 然后,用户可以向左滑动TextView
以加载下一个item
(下一个文本文档),或者向右滑动TextView
以查看List
中的上一个item
(上一个文本文档). NOTE: I have not implemented the swipe logic in code at this point in my app development.我发现,RecyclerView
添加了空格/行号,如下所示,使用了List
和15个项目:
- 如果我点击
list
中的第一个项目,文本文档打开时,在RecyclerView
顶部没有任何额外的空白—这是我想要的—但是项目的结尾有14个断点—文本文档中没有额外的空白 - 如果我点击列表中间的一个
item
,比如第8项,文本文档上方会有7个行号,最后会增加7个额外的行号 - 如果我点击列表中的最后
item
个,则文档顶部添加了大约14个行号,但文本末尾没有添加额外的行号 - 使用15
item
List's
执行测试但是如果我使用singleitem
(单个文本文档)创建一个新的List
,该文档将完美打开,并且文件文本上方或下方没有任何额外的空间 - 对于我的第
list
页(共15页)items
,点击前item
页,顶部没有任何空白,底部则全部空白.对于我的list
/15items
,点击list
中的最后item
,所有的空白都在顶部,而底部没有空白,文本结束,滚动相应地停止.
一百零二
当我向下滚动列表时,onBindViewHolder()
方法被多次调用,如果我rem
出"清除"代码:
//holder.bind("");
附加的行号似乎被添加到项目中. 同样在向上滚动时,额外的行中断持续存在,并且现在出现具有增加的空白量的相同文本文档item
的多个迭代. 因此,如果没有holder.bind("");
代码,RecyclerView
会在我上下滚动页面时动态添加文本文档的迭代.
I've tried the following approaches to address the issue, but none have worked:个
-
确保
onBindViewHolder()
内部的逻辑是高效的, 不会执行不必要的操作. -
已验证我的
ViewHolder
个实现是否正确地包含了视图 用新数据更新他们的内容 -
判断数据一致性,并正确通知适配器 改变使用
notifyDataSetChanged()
. -
在
onBindViewHolder
中移动了我的条件逻辑,所以我重置了TextView for every
项` -
我有
cleaned project
和rebuilt project
,但对问题没有影响
以下是我的onBindViewHolder()
方法:
@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("");
}
}
下面是扩展和显示文本的xml:
<?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" />
我将感谢任何关于如何解决这个问题的见解或建议. 我可能需要提供更多的代码—请建议其他什么可能是有用的.
谢谢你!