android 在RecyclerView中滚动文本视图

bxfogqkk  于 2022-11-03  发布在  Android
关注(0)|答案(6)|浏览(208)

我有一个RecyclerView

<android.support.v7.widget.RecyclerView
            android:id="@+id/activityRecicler"
            android:layout_width="match_parent"
            android:scrollbars="vertical"
            android:layout_height="wrap_content"
            app:stackFromEnd="true"
            />

我有一个行布局:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/description"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/solutions"/>

    </LinearLayout>

Package 内容可以工作,但我的问题是TextView和id描述可能会很长,所以,我需要一个滚动的TextView,我该怎么做呢?

5lhxktic

5lhxktic1#

将布局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="vertical">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>

    <TextView
        android:id="@+id/solutions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

然后在你的活动中:

recyclerView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        findViewById(R.id.scroll_view).getParent().requestDisallowInterceptTouchEvent(false);
        return false;
    }
});
scrollView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // Disallow the touch request for parent scroll on touch of child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});
anhgbhbe

anhgbhbe2#

我是Android的初学者,所以我不知道我的解决方案是否好。我已经按照@Payal的建议添加了Layout Row

<ScrollView
        android:id="@+id/childScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/solutions"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="long_text"
            android:maxLines="5"
            android:scrollbars="vertical"/>
    </ScrollView>

在我的ViewHolder

public ViewHolder(View itemView) {
            super(itemView);
            solutions = (TextView) itemView.findViewById(R.id.solutions);
            description = (TextView) itemView.findViewById(R.id.description);
            scrollable = (ScrollView) itemView.findViewById(R.id.childScroll);

            solutions.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });

//Enabling scrolling on TextView.

            solutions.setMovementMethod(new ScrollingMovementMethod());
        }

这是工作。我可以滚动RecyclerView中的TextView

oxalkeyp

oxalkeyp3#

在viewholder中对您的textview id尝试以下代码。

scrollable = (TextView)findViewById(R.id.textView1);  
//Enabling scrolling on TextView.
 scrollable.setMovementMethod(new ScrollingMovementMethod());

TextView布局中添加

android:scrollbars="vertical"
oxiaedzo

oxiaedzo4#

rowlayout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
<TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="#ff4500" />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/solutions"/>
</LenearLayout>

在活动中

descrptionTextview.setSelected(true);
t30tvxxf

t30tvxxf5#

或者你可以像这样把它 Package 在scrollview

<ScrollView 
    android:layout_height="100px" 
    android:layout_width="fill_parent"> 
    <TextView 
        android:id="@+id/text_view" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="long_text"/>
</ScrollView>


将这两个属性添加到yout textview

android:maxLines="5"
android:scrollbars="vertical"

并在代码中添加以下行

textview.setMovementMethod(new ScrollingMovementMethod());

希望这对你有帮助:)

dsf9zpds

dsf9zpds6#

创建新类

public class AutoScrollingTextView extends TextView {

    public AutoScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AutoScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoScrollingTextView(Context context) {
        super(context);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect     previouslyFocusedRect) {
        if (focused) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if (focused) {
            super.onWindowFocusChanged(focused);
        }
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

然后在xml中

<AutoScrollingTextView
    android:scrollHorizontally="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"/>

这很管用!!!

相关问题