如何使scrollTo停在我打算? [英] how to make scrollTo stop where I intend?

查看:171
本文介绍了如何使scrollTo停在我打算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图标(按钮)就可以了水平滚动条。

I have a horizontal scroll bar with icons (buttons) on it.

我尝试选择的图标移动到杆的中间

I try to move the selected icon to the middle of the bar.

我已阅读本<一个href=\"http://stackoverflow.com/questions/7773206/where-does-android-view-scrolltox-y-scroll-to\">post关于Android的定位的看法。

I have read this post about positioning views in android.

我有这个code,这似乎在逻辑上确定对我说:

I have got this code, which seems logically ok to me:

    public void selectButton() {
 ...
            HorizontalScrollView sv=(HorizontalScrollView)button.getParent().getParent();
            int offsetX=getButtonXPosition()-sv.getWidth()/2;
            sv.smoothScrollTo(offsetX, 0);

..
    }


    public int getButtonXPosition() {
        return (button.getLeft()+button.getRight())/2;
    }

我谨左上角 A 像素向右\\左(负\\正像素数),

I move left top corner a pixels to right\left (negative\positive number of pixels),

其中,选择按钮 中间的 = X位置 - 酒吧中间

这意味着我希望移动的按钮中间的 A 像素的权利\\左(负\\正)

meaning I want to move the middle of the button a pixels to the right\left (negative\positive)

不过按键停歇过左\\右(如果它是最右边的\\最左边的按钮)

However the buttons stop too left\right (if it's the rightmost\leftmost button)

见附件图片:

推荐答案

试试这个....

<HorizontalScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"  
        android:layout_margin="10dp">

    </LinearLayout>
</HorizontalScrollView>

结果

for (int x = 0; x < ImagesList.size(); x++) {
    imageHor = new ImageView(FullIMageScreenNew.this);
    linearLayout.setTag(x);
    imageHor.setImageBitmap(decodeSampledBitmapFromResource(ImagesList.get(x))); 
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    llp.setMargins(10, 10, 10, 10); // 4 margin values for Top/Left/Right/Bottom
    linearLayout.addView(imageHor, llp);
}

linearLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < ImagesList.size(); i++) {   
                indexNumber.add(i);
                int index = indexNumber.indexOf(i);
                Log.e("IndexValue=====", "" + index);
                awesomePager.setCurrentItem(index, true);
            }
        }
});

这篇关于如何使scrollTo停在我打算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆