我正在做自动水平滚动.所以我有15件物品.现在我想访问12项,所以我的索引是11.但我无法在索引发生时自动滚动它.
horizontalScrollView.scrollTo(12,0); @Override public void onPageSelected(int page) { for(int i = 0; i < holeTitle.length; i++) { if(i == page) { title[i].setTextColor(0xffffffff); horizontalScrollView.scrollTo(12,0); } else { title[i].setTextColor(0xffe0e0e0); } } }
请专家看看.
解决方法
DmRomantsov的答案是滚动到第12个按钮的正确方法.但是,getLeft()和getRight()方法返回0,因为屏幕上尚未显示布局.现在计算布局父级和子级的宽度还为时过早.要实现它,您需要在
onWindowFocusChanged
内进行自动滚动.
@Override public void onWindowFocusChanged(boolean hasFocus){ super.onWindowFocusChanged(hasFocus); if(hasFocus){ // do smoothScrollTo(...); } }
但是,在Fragment中,上面的方法不起作用.我只是写它来提供线索,了解这个概念.要在Fragment中具有相同的行为,您只需要执行一个Runnable,它可以显示您的UI时间.然后,使用面向水平的LinearLayout执行此操作:
// Init variables HorizontalScrollView mHS; LinearLayout mLL; // onCreateView method @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_container,container,false); // Find your views mHS = (HorizontalScrollView)view.findViewById(R.id.hscrollview); mLL = (LinearLayout)view.findViewById(R.id.hscrollview_container); // Do a Runnable on the inflated view view.post(new Runnable() { @Override public void run() { Log.v("","Left position of 12th child = "+mLL.getChildAt(11).getLeft()); mHS.smoothScrollTo(mLL.getChildAt(11).getLeft(),0); } }); return view; }
Middle HorizontalScrollView:
你的问题是自动滚动到你的第12个孩子.但是,在下面的评论中,您要求我在HorizontalScrollView的中间自动滚动,我假设在每个设备上.您需要计算屏幕的宽度,容器的总宽度以及设备宽度内显示的子项数.这是一个简单的代码:
// Auto scroll to the middle (regardless of the width screen) view.post(new Runnable() { @Override public void run() { // Width of the screen DisplayMetrics metrics = getActivity().getResources() .getDisplayMetrics(); int widthScreen = metrics.widthPixels; Log.v("","Width screen total = " + widthScreen); // Width of the container (LinearLayout) int widthContainer = mLL.getWidth(); Log.v("","Width container total = " + widthContainer ); // Width of one child (Button) int widthChild = mLL.getChildAt(0).getWidth(); Log.v("","Width child = " + widthChild); // Nb children in screen int nbChildInScreen = widthScreen / widthChild; Log.v("","Width screen total / Width child = " + nbChildInScreen); // Width total of the space outside the screen / 2 (= left position) int positionLeftWidth = (widthContainer - (widthChild * nbChildInScreen))/2; Log.v("","Position left to the middle = " + positionLeftWidth); // Auto scroll to the middle mHS.smoothScrollTo(positionLeftWidth,0); } }); /** * Your value might be resumed by: * * int positionLeftWidth = * ( mLL.getWidth() - ( mLL.getChildAt(0).getWidth() * * ( metrics.widthPixels / mLL.getChildAt(0).getWidth() ) ) ) / 2; * **/
具有所选值的Middle HorizontalScrollView:
我有点误解了真正的要求.实际上,您想要自动滚动直到选定的子视图,并在屏幕中间显示此视图.
然后,我更改了最后一个int positionLeftWidth,它现在引用所选视图相对于其父项的左侧位置,一个屏幕中包含的子项数以及所选视图的半宽.所以,除了positionLeftWidth之外,代码与上面相同:
// For example the chosen value is 7 // 7th Child position left int positionChildAt = mLL.getChildAt(6).getLeft(); // Width total of the auto-scroll (positionLeftWidth) int positionLeftWidth = positionChildAt - // position 7th child from left less ( ( nbChildInScreen // ( how many child contained in screen * widthChild ) / 2 ) // multiplied by their width ) divide by 2 + ( widthChild / 2 ); // plus ( the child view divide by 2 ) // Auto-scroll to the 7th child mHS.smoothScrollTo(positionLeftWidth,0);
然后,无论getChildAt()方法中的值如何,无论宽度屏幕如何,您始终都会在屏幕中间选择(在您的情况下)按钮.