Android 从屏幕中心开始和结束水平滚动视图

Android 从屏幕中心开始和结束水平滚动视图,android,horizontalscrollview,Android,Horizontalscrollview,我需要在水平滚动视图中开始和结束内容,就像我们在Gallery(弃用)小部件中所做的那样。水平滚动视图中的内容应该从屏幕中心开始,滚动时,它应该从中心向左移动,与Gallery相同 我有几百张图像,动态填充在水平滚动视图中的linearlayout中。我实现了以1秒的时间延迟自动滚动水平滚动视图。但我需要从中心开始,然后在中心结束 public class ImageGalleryScrollActivity extends AppCompatActivity { Hor

我需要在水平滚动视图中开始和结束内容,就像我们在Gallery(弃用)小部件中所做的那样。水平滚动视图中的内容应该从屏幕中心开始,滚动时,它应该从中心向左移动,与Gallery相同

我有几百张图像,动态填充在水平滚动视图中的linearlayout中。我实现了以1秒的时间延迟自动滚动水平滚动视图。但我需要从中心开始,然后在中心结束

public class ImageGalleryScrollActivity
        extends AppCompatActivity {
    HorizontalScrollView horizontalScrollView;
    int maxCount=59;
    Handler mHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            horizontalScrollView.smoothScrollBy(maxCount, 0);     

            mHandler.sendMessageDelayed(new Message(), 1000);
            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horizontal_scroll_activity);
        horizontalScrollView=(HorizontalScrollView)findViewById(R.id.horizontalView);
        mHandler.sendMessageDelayed(new Message(), 1000);     
    }   
}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:id="@+id/horizontalView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

您可以在scrollview上使用android:cliptoppadding=“false”属性组合左/右填充,每个填充等于屏幕宽度的一半

因为直到运行时才知道需要多少填充,所以必须用Java设置填充。将其放入您的
onCreate()


您可以将屏幕宽度的一半左右填充与scrollview上的
android:cliptoppadding=“false”
属性相结合

因为直到运行时才知道需要多少填充,所以必须用Java设置填充。将其放入您的
onCreate()


是否有方法在到达最后一个元素时再次启动autoscroll?是否有方法在到达最后一个元素时再次启动autoscroll?
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int padding = getResources().getDisplayMetrics().widthPixels / 2;
        scrollView.setPadding(padding, 0, padding, 0);
        scrollView.setClipToPadding(false);
    }
});