Android 水平滚动视图库。双向滚动或从开始滚动
我正在制作水平滚动视图库,我想自动滚动它。现在它是从左向右滚动的,但当我到达列表的末尾时,我只是跳到第一个,但它看起来非常糟糕,所以我想从开始滚动,避免只跳到第一个,或者如果不可能,在我到达右侧的最后一个视图时开始滚动到另一侧(可能更好的选择)。有人能帮我怎么做吗Android 水平滚动视图库。双向滚动或从开始滚动,android,scrollview,Android,Scrollview,我正在制作水平滚动视图库,我想自动滚动它。现在它是从左向右滚动的,但当我到达列表的末尾时,我只是跳到第一个,但它看起来非常糟糕,所以我想从开始滚动,避免只跳到第一个,或者如果不可能,在我到达右侧的最后一个视图时开始滚动到另一侧(可能更好的选择)。有人能帮我怎么做吗 private LinearLayout horizontalOuterLayout; private HorizontalScrollView horizontalScrollview; private int scrollMax;
private LinearLayout horizontalOuterLayout;
private HorizontalScrollView horizontalScrollview;
private int scrollMax;
private int scrollPos = 0;
private TimerTask clickSchedule;
private TimerTask scrollerSchedule;
private TimerTask faceAnimationSchedule;
private Timer scrollTimer = null;
private Timer faceTimer = null;
private String[] imageNameArray ={ "sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula","sponsors_czarnykot", "sponsors_estradarzeszow","sponsors_klubp","sponsors_kula" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontal_layout);
horizontalScrollview = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
horizontalOuterLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);
horizontalScrollview.setHorizontalScrollBarEnabled(false);
addImagesToView();
ViewTreeObserver vto = horizontalOuterLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
horizontalOuterLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getScrollMaxAmount();
startAutoScrolling();
}
});
}
public void getScrollMaxAmount()
{
int actualWidth = (horizontalOuterLayout.getMeasuredWidth() - 512);
scrollMax = actualWidth;
}
public void startAutoScrolling()
{
if (scrollTimer == null)
{
scrollTimer = new Timer();
final Runnable Timer_Tick = new Runnable()
{
public void run()
{
moveScrollViewRight();
}
};
if (scrollerSchedule != null)
{
scrollerSchedule.cancel();
scrollerSchedule = null;
}
scrollerSchedule = new TimerTask()
{
@Override
public void run()
{
runOnUiThread(Timer_Tick);
}
};
scrollTimer.schedule(scrollerSchedule, 30, 30);
}
}
public void moveScrollViewRight()
{
scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);
if (scrollPos >= scrollMax)
{
scrollPos = 0;
}
horizontalScrollview.scrollTo(scrollPos, 0);
}
/** Adds the images to view. */
public void addImagesToView()
{
for (int i = 0; i < imageNameArray.length; i++)
{
final ImageView imageView = new ImageView(this);
int imageResourceId = getResources().getIdentifier(imageNameArray[i], "drawable", getPackageName());
Drawable image = this.getResources().getDrawable(imageResourceId);
imageView.setBackgroundDrawable(image);
imageView.setTag(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(180, 123);
params.setMargins(0, 25, 0, 25);
imageView.setLayoutParams(params);
horizontalOuterLayout.addView(imageView);
}
}
public void stopAutoScrolling()
{
if (scrollTimer != null)
{
scrollTimer.cancel();
scrollTimer = null;
}
}
public void onBackPressed()
{
super.onBackPressed();
finish();
}
public void onPause()
{
super.onPause();
finish();
}
public void onDestroy()
{
clearTimerTaks(clickSchedule);
clearTimerTaks(scrollerSchedule);
clearTimerTaks(faceAnimationSchedule);
clearTimers(scrollTimer);
clearTimers(faceTimer);
clickSchedule = null;
scrollerSchedule = null;
faceAnimationSchedule = null;
scrollTimer = null;
faceTimer = null;
super.onDestroy();
}
private void clearTimers(Timer timer)
{
if (timer != null)
{
timer.cancel();
timer = null;
}
}
private void clearTimerTaks(TimerTask timerTask)
{
if (timerTask != null)
{
timerTask.cancel();
timerTask = null;
}
}
专用直线布局水平外侧布局;
私有水平滚动视图水平滚动视图;
私有int-scrollMax;
私有int-scrollPos=0;
私人时间任务点击时间表;
私人时间任务滚动时间表;
私人时间任务面动画时间表;
专用计时器scrollTimer=null;
专用计时器faceTimer=null;
私有字符串[]imageNameArray={“赞助者扎尔尼科夫”、“赞助者埃斯特拉达尔泽佐”、“赞助者克鲁布”、“赞助者库拉”、“赞助者扎尔尼科夫”、“赞助者埃斯特拉达尔泽佐”、“赞助者库拉”};
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局、水平布局);
horizontalScrollview=(horizontalScrollview)findViewById(R.id.horiztonal\u scrollview\u id);
水平外部布局=(线性布局)findViewById(R.id.horiztonal\u outer\u layout\u id);
horizontalScrollview.setHorizontalScrollBarEnabled(false);
addImagesToView();
ViewTreeObserver vto=horizontalOuterLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新的OnGlobalLayoutListener()
{
@凌驾
公共图书馆
{
horizontalOuterLayout.getViewTreeObserver();
getScrollMaxAmount();
startautoscroling();
}
});
}
public void getScrollMaxAmount()
{
int actualWidth=(horizontalOuterLayout.getMeasuredWidth()-512);
scrollMax=实际宽度;
}
公共空间STARTOTOSCROLING()
{
如果(scrollTimer==null)
{
scrollTimer=新计时器();
最终可运行计时器\u Tick=new Runnable()
{
公开募捐
{
moveScrollViewRight();
}
};
if(scrollerSchedule!=null)
{
scrollerSchedule.cancel();
scrollerSchedule=null;
}
scrollerSchedule=新时间任务()
{
@凌驾
公开募捐
{
RunUnuithRead(计时器滴答声);
}
};
scrollTimer.schedule(ScrollrSchedule,30,30);
}
}
public void moveScrollViewRight()
{
scrollPos=(int)(horizontalScrollview.getScrollX()+1.0);
如果(scrollPos>=scrollMax)
{
scrollPos=0;
}
scrollTo(scrollPos,0);
}
/**添加要查看的图像*/
公共无效addImagesToView()
{
对于(int i=0;i
以另一种方式向后滚动将是最简单的
添加一个设置为1.0的实例变量(称为sayscrollDist
)
那就换这一行
scrollPos=(int)(horizontalScrollview.getScrollX()+1.0)代码>
到
scrollPos=(int)(horizontalScrollview.getScrollX()+scrollDist)代码>
这条线呢
scrollPos=0代码>
到
scrollDist*=-1.0代码>
这样,每次点击scrollview的末尾时,它都会反转。对于无休止的滚动,请使用以下snnipet
public void getScrollMaxAmount() {
int actualWidth = (horizontalOuterLayout.getMeasuredWidth() - getWindowManager().getDefaultDisplay().getWidth());
scrollMax = actualWidth;
}
public void moveScrollView() {
// ********************* Scrollable Speed ***********************
scrollPos = (int) (horizontalScrollview.getScrollX() + 1.0);
if (scrollPos >= scrollMax) {
Log.v("childCount", ""+scrollMax);
addImagesToView();
getScrollMaxAmount();
}
horizontalScrollview.scrollTo(scrollPos, 0);
}