英文:
Automatically go to furthest right (last horizontal child) then back to furthest left(first child) in a horizontal scroll view
问题
我在一个水平的 LinearLayout 中有一些卡片,然后这个 LinearLayout 放在一个 HorizontalScrollView 中,我想要实现类似于图片幻灯片的左右旋转效果。
以下是我的 XML 代码,其中包含了 5 个卡片,它们在水平方向排列:
<HorizontalScrollView
android:id="@+id/horizontal_scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 1"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- ... Repeat similar CardView blocks for Product 2, 3, 4, and 5 ... -->
</LinearLayout>
</HorizontalScrollView>
我尝试使用了 <ViewFlipper>
等标签,但用户无法在布局中自由滚动。
英文:
I have cards in a horizontal LinearLayout which is then in a HorizontalScrollView
that I would like to rotate about left to right and back like an image slideshow.
Here is my XML, it contains 5 cards which are in horizontal alignment
<HorizontalScrollView
android:id="@+id/horizontal_scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 1"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 2"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 3"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 4"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@drawable/testimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:gravity="end"
android:text="USD 300"
android:textAlignment="viewEnd"
android:textColor="#ce375e"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginBottom="8dp"
android:text="Product 5"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
I have tried using tags such as <ViewFlipper
with that users cannot scroll themselves within the layout.
答案1
得分: 0
使用计时器进行连续滚动,并使用canScrollHorizontally()
来检查是否已滚动到最右边,然后开始向左滚动,
canScrollHorizontally()
使用正整数表示向右方向,负整数表示向左方向
private HorizontalScrollView scrollView;
private boolean scrollingLeft;
int pace = 50;
private Timer timer;
timer = new Timer("horizontalScrollViewTimer");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (scrollView.canScrollHorizontally(1) && !scrollingLeft ) {
Log.d(TAG, "向右滚动");
scrollView.smoothScrollBy(pace, 0);
}
else {
Log.d(TAG, "向左滚动");
scrollingLeft = true;
scrollView.smoothScrollBy(-pace, 0);
if (!scrollView.canScrollHorizontally(-1)) // 已到达最左侧,停止向左滚动
scrollingLeft = false;
}
}
});
}
}, 3000, 50);
英文:
Use a timer for continuous scrolling, and using canScrollHorizontally()
to check wether it reached the furthest right then start scrolling left,
canScrollHorizontally()
takes positive int for right direction, negative int for left direction
private HorizontalScrollView scrollView;
private boolean scrollingLeft;
int pace = 50;
private Timer timer;
timer = new Timer("horizontalScrollViewTimer");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (scrollView.canScrollHorizontally(1)&&!scrollingLeft ) {
Log.d(TAG, "Scrolling right");
scrollView.smoothScrollBy(pace, 0);
}
else {
Log.d(TAG, "Scrolling left");
scrollingLeft =true;
scrollView.smoothScrollBy(-pace, 0);
if(!scrollView.canScrollHorizontally(-1)) // reached the furthest left turn of scrolling left
scrollingLeft = false;
}
}
});
}
}, 3000, 50);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论