如何使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.

我已阅读本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);
            }
        }
});