android recyclerview-TextView使用setmaxlines方法无法正确扩展

问题描述 投票:0回答:1

我是android开发的新手。为了我的学习目的,我正在开发一个Android应用程序以在回收者视图中列出斐波那契数。当用户向下滚动到“回收者”视图时,该列表将附加新的数字。The image shows the app displaying the index and respective fibonacci number for the index in the recycler view

这是回收站视图中单个项目的布局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视图中重新使用同一视图,但没有任何帮助。

android android-recyclerview textview android-linearlayout animator
1个回答
0
投票

如果您想为文本视图输入多行,请将其更改为:

<TextView
        android:id="@+id/info_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textMultiLine"
        // this is the number of lines you want to display
        android:minLines="5"
        android:background="#CDD6D5"
        android:gravity="center"
        android:maxLines="1" />
© www.soinside.com 2019 - 2024. All rights reserved.