[英]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 这可能取决于不同的原因,但主要原因有两个
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)
Level
class. 'int selectedLevel'保存当前选定的级别,信息保存在Level
类中。 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.