【问题标题】:how to make scrollTo stop where I intend?如何让 scrollTo 停在我想要的地方?
【发布时间】:2014-07-03 16:13:02
【问题描述】:

我有一个带有图标(按钮)的水平滚动条。

我尝试将选定的图标移动到栏的中间。

我已阅读post 关于在 android 中定位视图的内容。

我有这个代码,在我看来逻辑上没问题:

    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 像素移动到右\左(负\正像素数),

其中a = X position of middle of selected button - middle of the bar

意思是我想将按钮的中间a像素移动到右\左(负\正)

但是按钮停得太左\右(如果它是最右边\最左边的按钮)

见附件:

【问题讨论】:

  • 也许你应该使用smoothScrollBy?您似乎在计算差异而不是实际位置。
  • @user1873880 我认为你是对的。但是a)它相对于(0,0)是不同的,所以没关系。 b)smoothScrollBy 让事情变得更加混乱

标签: java android xml user-interface scrollview


【解决方案1】:

试试这个....

<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);
            }
        }
});

【讨论】:

  • 你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
相关资源
最近更新 更多