获取子位置HorizontalScrollView并滚动到它 [英] Get child location in HorizontalScrollView and scroll to it
问题描述
貌似我无法获取或设置任何位置或滚动量在我简单的布局:
//相对布局 - main_layout
< HorizontalScrollView
机器人:ID =@ + ID / MenuScroll
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:fillViewport =真
机器人:滚动条=无>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =48dp
机器人:方向=横向
机器人:layout_gravity =中心
机器人:填充=0dp>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =150dp
机器人:layout_height =FILL_PARENT/>
<按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =150dp
机器人:layout_height =FILL_PARENT/>
<按钮
机器人:ID =@ + ID /按钮3
机器人:layout_width =150dp
机器人:layout_height =FILL_PARENT/>
<按钮
机器人:ID =@ + ID /将Button4
机器人:layout_width =150dp
机器人:layout_height =FILL_PARENT/>
<按钮
机器人:ID =@ + ID / button5
机器人:layout_width =150dp
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
< / HorizontalScrollView>
//结束RelativeLayout的的 - main_layout
我想什么我 MainActivity
是获得任何按钮的 HorizontalScrollView
里面的位置,然后滚动至位置。目前,我不能让左值(它总是0),如果我尝试 scrollTo
的 HorizontalScrollView
它只是不,不管什么x和y的值。
//进口
公共类MainActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main_layout);
最终按钮按钮3 =(按钮)findViewById(R.id.button3);
矩形rectf =新的矩形();
button3.getLocalVisibleRect(rectf);
Log.i(左,将String.valueOf(rectf.left)); //它始终是0
HorizontalScrollView MenuScroll =(HorizontalScrollView)findViewById(R.id.MenuScroll);
//试图滚动到X = 100,但它不
MenuScroll.scrollTo(100,MenuScroll.getBottom());
}
}
所以,正如我所说,我不能让左位为我的按钮,或者可以滚动到一些X位置我 HorizontalScrollView
。这可能吗?
(它总是0)
请记住,意见没有在的onCreate
方法尺寸它们被放置在的onCreate $在屏幕上C $ C>回调的回报。
我想在我的MainActivity是让任何按钮的位置 里面的HorizontalScrollView并滚动到该位置。
看一看下面的code(放在的onCreate
法):
最后HorizontalScrollView MenuScroll =(HorizontalScrollView)findViewById(R.id.MenuScroll);
MenuScroll.post(新的Runnable(){
@覆盖
公共无效的run(){
最终按钮按钮3 =(按钮)findViewById(R.id.button3);
INT scrollTo = 0;
最终诠释数=((的LinearLayout)MenuScroll.getChildAt(0))
.getChildCount();
的for(int i = 0; I<计数;我++){
最后查看孩子=((的LinearLayout)MenuScroll.getChildAt(0))
.getChildAt(ⅰ);
如果(孩子!=按钮3){
scrollTo + = child.getWidth();
} 其他 {
打破;
}
}
MenuScroll.scrollTo(scrollTo,0);
}
});
另外要小心你在布局中使用的属性(我指的是 layout_gravity
这可能导致问题,如果我没有记错)。
Looks like I can't get or set any position or scrolling amount in my simple layout:
// Relative layout - main_layout
<HorizontalScrollView
android:id="@+id/MenuScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="0dp" >
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button3"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button4"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button5"
android:layout_width="150dp"
android:layout_height="fill_parent" />
</LinearLayout>
</HorizontalScrollView>
// End of RelativeLayout - main_layout
What I want in my MainActivity
is to get the position of any button inside the HorizontalScrollView
and scroll to that position. At the moment I can't get the left value (it's always 0) and if I try to scrollTo
the HorizontalScrollView
it just doesn't, regardless of what the value for x or y is.
// imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final Button button3 = (Button)findViewById(R.id.button3);
Rect rectf = new Rect();
button3.getLocalVisibleRect(rectf);
Log.i("Left: ", String.valueOf(rectf.left)); // it is always 0
HorizontalScrollView MenuScroll = (HorizontalScrollView)findViewById(R.id.MenuScroll);
// Trying to scroll to X=100 but it doesn't
MenuScroll.scrollTo(100, MenuScroll.getBottom());
}
}
So, as I said, I can't get the left position for my button and either can scroll to some X position in my HorizontalScrollView
. Is this possible?
(it's always 0)
Keep in mind that the views don't have dimensions in the onCreate
method as they are placed on the screen after the onCreate
callback returns.
What I want in my MainActivity is to get the position of any button inside the HorizontalScrollView and scroll to that position.
Have a look at the code below(placed in the onCreate
method):
final HorizontalScrollView MenuScroll = (HorizontalScrollView) findViewById(R.id.MenuScroll);
MenuScroll.post(new Runnable() {
@Override
public void run() {
final Button button3 = (Button) findViewById(R.id.button3);
int scrollTo = 0;
final int count = ((LinearLayout) MenuScroll.getChildAt(0))
.getChildCount();
for (int i = 0; i < count; i++) {
final View child = ((LinearLayout) MenuScroll.getChildAt(0))
.getChildAt(i);
if (child != button3) {
scrollTo += child.getWidth();
} else {
break;
}
}
MenuScroll.scrollTo(scrollTo, 0);
}
});
Also be careful with the attributes you use in the layout(I'm referring to the layout_gravity
which could cause problems if I'm not mistaken).
这篇关于获取子位置HorizontalScrollView并滚动到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!