首页 > 解决方案 > 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代码中的一个错误。但只想在这里确认我可能错过的任何解决方案。

标签: androidandroid-recyclerviewtextviewandroid-linearlayoutanimator

解决方案


我能够在我的设备上重复您的问题,您遇到的问题是您的 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 中


推荐阅读