简体   繁体   English

以编程方式将ScrollView聚焦到选定位置 - Android

[英]Focus ScrollView to selected position programmatically - Android

I have a ScrollView with a lot buttons. 我有一个带有很多按钮的ScrollView。 Each button is enabled when the user unlocks that button/level. 当用户解锁该按钮/级别时,每个按钮都会启用。 I would like to focus the ScrollView on the latest unlocked button/level. 我想将ScrollView集中在最新的解锁按钮/关卡上。 See the screenshot below. 请参见下面的截图。

在此输入图像描述

I found some functions like scrollTo() that could focus the ScrollView either at top, button or something like that but I would like to focus the ScrollView at certain places like the button ID that says Level 8 in the screenshot below. 我找到了一些函数,比如scrollTo()可以将ScrollView聚焦在顶部,按钮或类似的东西但是我想将ScrollView聚焦在某些地方,比如下面屏幕截图中的Level 8所示的按钮ID。 How would I go about doing this? 我该怎么做呢?

I think this should be good enough : 我认为这应该足够好了:

  yourButtonView.getParent().requestChildFocus(yourButtonView,yourButtonView);

public void RequestChildFocus (View child, View focused) public void RequestChildFocus(查看子项,查看焦点)

child - The child of this ViewParent that wants focus. child - 此ViewParent的子项需要焦点。 This view will contain the focused view. 该视图将包含焦点视图。 It is not necessarily the view that actually has focus. 实际上并不一定是关注焦点的观点。

focused - The view that is a descendant of child that actually has focus 专注 -这是孩子的后代,实际上有焦点的视图

First you need to know the position of item in scroll that has to get focus once you know that you can use following code to make that item focus 首先,您需要知道滚动中项目的位置,一旦您知道可以使用以下代码使该项目成为焦点,就必须获得焦点

    final int x;
            final int y;
            x = rowview[pos].getLeft();
            y = rowView[pos].getTop();

                yourScrollView.scrollTo(x, y);

refer this question 提到这个问题

I agree with the benefits of previous answers. 我同意以前答案的好处。 However, by experience, I have found this to be more complex than this. 但是,根据经验,我发现这比这更复杂。 The setSelectionFromTop is very sensitive and often breaks if it is executed too early. setSelectionFromTop非常敏感,如果执行得太早,通常会中断。 This may depend on different reasons but the two primary reasons are that 这可能取决于不同的原因,但主要原因有两个

  • If executed from within Activity lifecycle methods the views have not been loaded/configured yet. 如果从Activity生命周期方法中执行,则尚未加载/配置视图。
  • View modifications triggered after the list move action seems to break the move. 查看列表移动操作后触发的修改似乎打破了此举。 Probably overwriting some value before the move has been finalized due to a recalculation of the views. 由于重新计算了视图,在移动完成之前可能会覆盖某些值。

The reasoning seems to apply for both setSelectionFromTop and setSelection() methods although I tested mostly with setSelectionFromTop. 虽然我主要使用setSelectionFromTop测试,但推理似乎同时适用于setSelectionFromTop和setSelection()方法。 smoothScrollToPosition() seems to be more robust, probably because it by definition changes the list position delayed whn doing the smooth scrolling. smoothScrollToPosition()似乎更健壮,可能是因为它根据定义更改了列表位置延迟了做平滑滚动。

Look at this example source code: 看看这个示例源代码:

// Wee need to pospone the list move until all other view setup activities are finished
list.post(new Runnable(){
    @override
    public void run() {
        list.setSelectionFromTop(selectedPosition, Math.max(0, Math.round(list.getHeight() / 3))); // Make sure selection is in middle of page, if possible.
        // Make sure you do not modify (this or other) parts of the view afterwards - it may break the list move
        // activityButtons.setVisibility(View.GONE);
}});

see: http://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int , int) 请参阅: http//developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop (int,int)

  • 'int selectedLevel' holds the currently selected level, information is held in a Level class. 'int selectedLevel'保存当前选定的级别,信息保存在Level类中。
  • 'values' is a list with all your levels, displayed by the list. 'values'是一个包含所有级别的列表,由列表显示。

Example: 例:

    ListView lv = (ListView)findViewById(android.R.id.list);
    int i = 0;
    for (Level l : values) {
        i++;
        if (l.level == selectedLevel) {
            lv.setSelectionFromTop(i, 200);
            break;
        }
    }

try this 尝试这个

sc.post(new Runnable() { 
        public void run() { 
             sc.smoothScrollTo(x, y);
        } 
    });

x the position where to scroll on the X axis x在X轴上滚动的位置

y the position where to scroll on the Y axis y在Y轴上滚动的位置

Use this code, taken from this answer 使用此代码,取自此答案

private final void focusOnView(){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                your_scrollview.scrollTo(0, your_EditBox.getBottom());
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式滚动 ScrollView 以专注于按钮或视图 - Android - Programmatically scroll a ScrollView to focus on a Button or a view - Android 我可以在 Android 中以编程方式滚动 ScrollView 吗? - Can I scroll a ScrollView programmatically in Android? 如何在Android中以编程方式添加焦点? - How can I add focus programmatically in Android? Android / Java:EditText焦点会移动整个布局,只有ScrollView应该是 - Android/Java: EditText focus moves whole layout, and only ScrollView should be 以编程方式创建带有 2 列按钮的滚动视图 Android Studio Java - Programmatically create a scrollview with 2 columns of buttons Android Studio Java 无法在Android中显示ScrollView和textView(以编程方式添加) - unable to make the ScrollView and textView (added programmatically) visible in Android 在ScrollView(ANDROID)内的无限RecycleView中保留滚动位置 - Retain scroll position in infinite RecycleView inside ScrollView (ANDROID) Android 在 RelativeLayout 中以编程方式切换项目位置 - Android switch item position programmatically in a RelativeLayout Android-以编程方式操纵ListView下方的按钮位置 - Android -Programmatically Manipulate Button Position below ListView Android:以编程方式更改TextView位置(向左或向右)? - Android: Changing TextView Position (Left or Right) Programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM