我有一组横幅图片可以在用户滑动时滑动,触摸图像会重定向到某个网站.. 我想为横幅图片添加自动滚动功能,但我不太清楚该怎么做,任何人都可以帮我看看下面的代码吗?
public class MyAdapter extends InfinitePagerAdapter {
BannerPagerAdapter pagerAdapter;
AutoScrollViewPager viewPager;
public static ArrayList<HashMap<String,String>> bannerAry = new ArrayList<HashMap<String,String>>();
public void onCreate(Bundle savedInstanceState) {
// Home header banner
LayoutInflater inflater = getLayoutInflater();
header = inflater.inflate(R.layout.home_banner, null);
if (homeBanner.equalsIgnoreCase("enable") && bannerAry.size() > 0){
gridView.addHeaderView(header);
}
viewPager = (AutoScrollViewPager) header.findViewById(R.id.view_pager);
pageIndicator = (LinePageIndicator) header.findViewById(R.id.indicator);
display = this.getWindowManager().getDefaultDisplay();
float scale = (float) display.getWidth() / Constants.HOME_BANNER_WIDTH;
int newHeight = (int) Math.round(Constants.HOME_BANNER_HEIGHT * scale);
viewPager.getLayoutParams().height = newHeight;
pagerAdapter = new BannerPagerAdapter(this, bannerAry);
viewPager.setAdapter(pagerAdapter);
pageIndicator.setViewPager(viewPager);
viewPager.addOnPageChangeListener(mOnPageChangeListener);
class BannerPagerAdapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String,String>> data;
public BannerPagerAdapter(Context act, ArrayList<HashMap<String,String>> newary) {
this.data = newary;
this.context = act;
}
public int getCount() {
return data.size();
}
public Object instantiateItem(ViewGroup collection, final int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.banner_image,
collection, false);
ImageView image = (ImageView) itemView.findViewById(R.id.image);
String img = data.get(position).get("image");
Log.v("banner img", "img="+img);
if (!img.equals("")){
Picasso.with(FragmentMainActivity.this).load(img).into(image);
}
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Patterns.WEB_URL.matcher(data.get(position).get("url")).matches()) {
Intent b = new Intent(Intent.ACTION_VIEW, Uri.parse(data.get(position).get("url")));
startActivity(b);
} else {
Toast.makeText(FragmentMainActivity.this, getString(R.string.url_invalid), Toast.LENGTH_SHORT).show();
}
}
});
((ViewPager) collection).addView(itemView, 0);
return itemView;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int state) {
enableDisableSwipeRefresh( state == ViewPager.SCROLL_STATE_IDLE );
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
}
};
private void enableDisableSwipeRefresh(boolean enabled){
if (enabled) {
swipeLayout.setEnabled(true);
} else {
swipeLayout.setEnabled(false);
}
}
答案 0 :(得分:0)
您可以使用TimerTask来执行此操作,您可以在其中安排具有一定持续时间间隔的滚动事件,您需要在初始化viewPager和pagerAdapter之后调用此方法,这里是代码:
private void startAutoScrolling() {
stopAutoScrolling();
TimerTask task = new TimerTask() {
@Override
public void run() {
if(activityContext!=null)
activityContext.runOnUiThread(new Runnable() {
@Override
public void run() {
int position = (viewPager.getCurrentItem() + 1) % pagerAdapter.getCount();
viewPager.setCurrentItem(position);
}
});
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task, 3000, 3000);
}
这里是stopAutoScrolling()方法,以确保你停止以前运行的TimerTask进行自动滚动,如果用户在按下主页按钮或接听电话后返回应用程序可能就是这种情况。
public void stopAutoScrolling() {
Log.i(TAG, "stop auto scroll of viewpager");
if (timer != null)
timer.cancel();
}