GVKun编程网logo

android – Scrollable TextView不允许在应用程序暂停后选择文本(textview设置不可点击)

7

对于android–ScrollableTextView不允许在应用程序暂停后选择文本感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解textview设置不可点击,并且为您提供关于Andro

对于android – Scrollable TextView不允许在应用程序暂停后选择文本感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解textview设置不可点击,并且为您提供关于Android 1父级ScrollView,3垂直滚动的TextView、Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决、android ScrollerView嵌套ExpandalListView的高度问题、Android ScrollView scrollTo()方法无法使用TextView?的宝贵知识。

本文目录一览:

android – Scrollable TextView不允许在应用程序暂停后选择文本(textview设置不可点击)

android – Scrollable TextView不允许在应用程序暂停后选择文本(textview设置不可点击)

我有一个可滚动的TextView,用户可以在其中选择文本.我通过将移动方法设置为ScrollingMovementMethod来添加滚动条.

问题:除非应用程序暂停(例如,在切换应用程序之后),否则选择工作正常.一旦应用程序处于活动状态,选择将停止工作,并在日志中收到以下消息:

W/TextView: TextView does not support text selection. Selection
cancelled.

我的设置:

我有一个带有CoordinatorLayout的Activity和一个带有TextView的Fragment,它包含在RelativeLayout中,如下所示:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:scrollbars="vertical" />

在Java代码中我必须这样做:

textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTextIsSelectable(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);

因为根据this,this和this问题,这是唯一的工作方式.

编辑:

问题出在以下调用中

textView.setMovementMethod(new ScrollingMovementMethod());

如果我删除它有效,但我无法理解.

重现问题的最小步骤:

1)使用以下布局使用TextView创建一个空的Activity.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:text="Some very very very long text..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:scrollbars="vertical" />

</android.support.design.widget.CoordinatorLayout>

2)在onStart()方法中设置TextView的可见性参数.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        TextView textView = findViewById(R.id.text_view);
        textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setTextIsSelectable(true);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
    }
}

3)尝试在暂停应用程序之前和之后使用TextView上的上下文菜单.

编辑2:

删除setMovementMethod(new ScrollingMovementMethod())解决了我的问题,之后功能正常.但是我不太清楚为什么它被添加了,我担心如果我删除它会制造一些东西.知道为什么人们可能会将ScrollingMovementMethod与android结合使用:scrollbars =“vertical”.可能是xml在某些情况下不起作用?想法?我仍然有兴趣使用ScrollingMovementMethod制动器选择功能?

解决方法

设置ScrollingMovementMethod使TextView能够“按自己”滚动,例如当您设置非常长的文本时,它会在底部或边缘切割.使用ScrollingMovementMethod可以滚动TextView,不需要将它放在可滚动的容器中,例如在ScrollView或Horizo​​ntalScrollView中

android:scrollbars =“vertical”行说如果这个View得到“scrollablility”(例如通过上面的移动方法)那么UI应该只显示垂直滚动条.来自docs:

Defines which scrollbars should be displayed on scrolling or not.

它是View个文档,特别是TextView,因为很少扩展“种类”的视图可以获得“scolllability”,包括所有ViewGroups,如ScrollView,ListView,RecyclerView等.

最后这行在你的代码中做了什么?在setTextIsSelectable内你有这条线:

setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);

所以实际上它的覆盖移动方法你在自己的代码中设置了几行.我敢打赌,前段时间你的TextView“可以自行分辨”,而且有一天,一些聪明的家伙重写了这个并将TextView放入例如XML中的ScrollView和移动方法保留在代码中.

textIsSelectable工作直到Activity暂停,因为恢复后你(再次)设置ScrollableMovementMethod,但是在setTextIsSelectable里面你有

if (mEditor.mTextIsSelectable == selectable) return;

您在活动暂停之前首次运行时设置了mTextIsSelectable标志,并且此标志已恢复,因此不会触发下面的代码(因此不会使用ArrowKeyMovementMethod重置移动方法,并且您的ScrollableMovementMethod保持不变).所以回答问题,这条线在你的代码中做了什么:它在暂停和恢复Activity之后打破了“可选择性”,除此之外什么都没有

来自ScrollingMovementMethodArrowKeyMovementMethod的消息来源:仅在ArrowKeyMovementMethod(在上面的setTextIsSelectable中设置为移动方法)中,您已经覆盖了onTouchEvent方法,并在其中部分处理选择的行

编辑:请注意,在setTextIsSelectable中你设置了“focusability”,所以这些行是不必要的:

textView.setFocusable(true);
textView.setFocusableInTouchMode(true);

所以你可以将你的代码缩短到一行:

textView.setTextIsSelectable(true);

或删除所有引用的Java代码并添加一行XML行:

android:textIsSelectable="true"

Android 1父级ScrollView,3垂直滚动的TextView

Android 1父级ScrollView,3垂直滚动的TextView

我对android编程有疑问.

我有一个用于整个活动的父滚动视图,并且有三个具有滚动功能的文本视图.但是,当我使用以下代码时,它似乎根本无法工作.仅父级滚动可用.

        final View view = inflater.inflate(R.layout.activity_register_terms_fragment, container, false);
        TextView basicTermView = (TextView) view.findViewById(R.id.register_terms_basic_info);
        TextView purposeTermView = (TextView) view.findViewById(R.id.register_terms_purpose_info);
        TextView provideTermView = (TextView) view.findViewById(R.id.register_terms_provide_info);
        TextView prevIoUs = (TextView) view.findViewById(R.id.register_terms_pre);
        TextView next = (TextView) view.findViewById(R.id.register_terms_next);

        basicTermView.setMovementMethod(new ScrollingMovementMethod());
        purposeTermView.setMovementMethod(new ScrollingMovementMethod());
        provideTermView.setMovementMethod(new ScrollingMovementMethod());

我应该如何更改密码?
感谢你们对我的帮助!

解决方法:

ScrollView内不能有可滚动的视图,例如TextView或ListView或RecyclerView.因此,可以在常规布局中使用简单的TextView并为其添加android:scrollbars属性,也可以使用视图的自定义类,该类将以编程方式计算视图的宽度/高度,并使用ScrollView作为其父级.

例如,要在滚动视图中使用Listview,我们需要使用Listview的以下自定义类,该类将计算列表项的高度并对其进行设置.

public class ExpandedListView extends ListView {

private ViewGroup.LayoutParams params;
private int old_count = 0;

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

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

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

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();    
}
@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        this.setScrollContainer(false);
        old_count = getCount();
        params = getLayoutParams();
        params.height = getCount()
                * (old_count > 0 ? getChildAt(0).getHeight() : 0);
        setLayoutParams(params);
    }

    super.onDraw(canvas);
}

}

Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决

Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决

①NestedScrollView与RecyclerView嵌套,导致滚动惯性消失

解决:mRecyclerView.setNestedScrollingEnabled(false); 

②NestedScrollView中嵌套其他布局和RecyclerView后,不会滚动到屏幕顶部

解决给最顶部的布局View设置下requestFocus();

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/src"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true">


            <LinearLayout
                android:id="@+id/ll_valuation_start"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:src="@drawable/ic_launcher_background"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="信用可也换钱花"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="6dp"
                    android:text="自营贷款产品,无需审核,立即放款"
                    android:textColor="@android:color/black"/>

                <android.support.v7.widget.RecyclerView

                    android:id="@+id/rv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                </android.support.v7.widget.RecyclerView>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

如果里面ScrollView里面有连个RecyclerView则都要是设置 mRecyclerView.setNestedScrollingEnabled(false);

android ScrollerView嵌套ExpandalListView的高度问题

android ScrollerView嵌套ExpandalListView的高度问题

在ScrollerView中嵌套一个listview或者ExpandalListView的时候,会发现,如果不去动态的计算listview和ExpandalListView的高度的话,只会显示出来一个item。

所以,需要通过一个方法去动态设置listview的高度:这里的参数可以是listview也可以是ExpandalListView。

private void setListViewHeight(ExpandableListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    totalHeight = 0;
    int count = listAdapter.getCount();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);

        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

 

但是ExpandalListView与listview不同,他会有点击展开的childview,如果我们仅仅用了上面的方法,你会发现,所有的item确实都显示出来了,但是当你点击groupView去显示childview的时候,你会发现,高度不够了,展开的childview把下面的原来的item挤了一部分出去。所以,我在groupView的点击监听中添加了对childview的高度的计算,如果展开,总的ExpandalListView的高度加上一个childview的高度,如果关闭,则减去一个。这里,用了一个全局变量来保存当前ExpandaListView的高度。

exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        if(parent.isGroupExpanded(groupPosition)){
            parent.collapseGroup(groupPosition);
            View childView = LayoutInflater.from(context).inflate(R.layout.tianxianbao_expandlist_item, null);
            childView.measure(0,0);
            totalHeight -= childView.getMeasuredHeight();
        }else{
            //第二个参数false表示展开时是否触发默认滚动动画
            parent.expandGroup(groupPosition,false);
            View childView = LayoutInflater.from(context).inflate(R.layout.tianxianbao_expandlist_item, null);
            childView.measure(0,0);
            totalHeight += childView.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = parent.getLayoutParams();
        params.height = totalHeight
                + (parent.getDividerHeight() * (parent.getCount() - 1));
        parent.setLayoutParams(params);
        parent.requestLayout();

        //telling the listView we have handled the group click, and don''t want the default actions.
        return true;
    }
});

Android ScrollView scrollTo()方法无法使用TextView?

Android ScrollView scrollTo()方法无法使用TextView?

例如,我有这样的布局:
<ScrollView>
  <LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         <TextView>
         </TextView>
    </LinearLayout>
  </LinearLayout>
</ScrollView>

我的TextView内容很长.现在我可以手动滚动ScrollView到TextView内容的末尾.在代码中,我写道:scrview.scrollTo(0,800);,scrview是我的ScrollView.它应该滚动到TextView内容的中间.但是在运行时,它只会滚动到TextView的开头,无法滚动TextView内容.

有人知道导致这个问题的原因吗?

编辑:我找到了.好像我太早叫做scrollTo了.运用

scrview.post(new Runnable() {
                public void run() {
                    scrview.scrollTo(0,800);
                }
            });

相反,它的工作原理.为任何遇到同样问题的人发布答案.

解决方法

首先,如果所有的LinearLayouts具有相同的方向,为什么不只是创建一次? 如果我没错,ScrollView只能有1个孩子(1个直接孩子).也许这可能是你遇到问题的原因. 但是,如果你仍然需要这3个LinearLayouts,你可以创建一个FrameLayout作为父级来保存它们并使FrameLayout成为ScrollView的子级

我们今天的关于android – Scrollable TextView不允许在应用程序暂停后选择文本textview设置不可点击的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android 1父级ScrollView,3垂直滚动的TextView、Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决、android ScrollerView嵌套ExpandalListView的高度问题、Android ScrollView scrollTo()方法无法使用TextView?的相关信息,请在本站查询。

本文标签: