android - android recyclerview - 使用 setmaxlines 方法无法正确扩展 TextView
问题描述
我是android开发的新手。为了我的学习目的,我正在开发一个 android 应用程序来在回收站视图中列出斐波那契数。当用户向下滚动回收站视图时,该列表会附加新数字。
该图显示了应用程序在回收站视图中显示索引和索引的相应斐波那契数
这是回收站视图中单个项目的布局 xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:focusable="false"
android:padding="5dp">
<TextView
android:id="@+id/info_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#CDD6D5"
android:gravity="center"
android:maxLines="1" />
<TextView
android:id="@+id/info_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CDD6D5"
android:gravity="center"
android:maxLines="1" />
</LinearLayout>
适配器类的摘录,
public class ViewHolder extends RecyclerView.ViewHolder {
final TextView index;
final TextView value;
ViewHolder(View itemView) {
super(itemView);
index = itemView.findViewById(R.id.info_index);
value = itemView.findViewById(R.id.info_value);
}
}
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
final TextView value = holder.value;
final TextView index = holder.index;
value.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
if (value.getMaxLines() == 1000) {
value.setMaxLines(1);
notifyItemChanged(position);
} else {
value.setMaxLines(1000);
notifyItemChanged(position);
}
}
});
Log.d(TAG, "position-value:" + String.valueOf(position));
holder.index.setBackgroundColor(Color.WHITE);
holder.index.setText(String.valueOf(position + 1));
holder.value.setBackgroundColor(Color.WHITE);
holder.value.setText(mData.get(position));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
holder.index.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
holder.value.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
}
}
当我单击值 textview 时,第一次单击时 textview 不会展开,而是我必须按两次才能使其展开。第一次只发生闪烁。我已经尝试禁用 recyclerview 的动画器,尝试在 recycler 视图中重新使用相同的视图,但没有任何帮助。
我的要求是在点击时扩展斐波那契值文本视图。默认情况下,它应该是 1 行,单击时它应该显示多行的整个内容(根据需要多行)。
目前这发生在两次点击。第一次闪烁,第二次扩展。
我相信这是android代码中的一个错误。但只想在这里确认我可能错过的任何解决方案。
解决方案
我能够在我的设备上重复您的问题,您遇到的问题是您的 LinearLayout 在到达您的 textview 之前正在消耗您的点击。
包括:
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"
在您的 LinearLayout 中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"
android:padding="5dp">
<TextView
android:id="@+id/info_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#CDD6D5"
android:gravity="center"
android:maxLines="1" />
<TextView
android:id="@+id/info_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CDD6D5"
android:gravity="center"
android:maxLines="1" />
</LinearLayout>
还包括:
android:nestedScrollingEnabled="false"
在您的 RecyclerView 中
推荐阅读
- reactjs - 在 ReactJs 中单击时设置活动单元格
- python - 使用 Python 脚本重命名 json 文件中的值
- google-chrome - 如何在 devtools 图中标记事件?
- arrays - 在空列表飞镖的特定索引处插入值
- swiftui - 如何在全局范围内绝对定位子视图
- firebase-realtime-database - 在数据查看器中激活只读和非实时模式以提高浏览器性能 || 现在数据库工作很慢
- flutter - Flutter:未处理的异常:刷新页面时发生在dispose()之后调用的setState()
- excel - 从具有多个页面的网站中抓取数据
- authorization - 在 HCL OneTest Data 中,admin 用户是否有权更改用户的权限?
- apk - 在新的 Google Play 控制台 UI 中找不到“编辑发布”选项