【发布时间】:2018-08-17 12:58:15
【问题描述】:
我需要在水平滚动视图中开始和结束内容,就像我们在画廊(已弃用)小部件中所做的那样。 Horizontal scrollview 内的内容应该从屏幕中心开始,滚动时应该从中心向左移动,与 Gallery 相同。
我有几百张图片,动态填充在水平滚动视图内的线性布局中。我实现了以一秒的时间延迟自动滚动水平滚动视图。但我需要从中心开始内容并以中心结束。
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>
【问题讨论】:
标签: android horizontalscrollview