gpt4 book ai didi

java - 使横幅图像自动滚动 - Android-Java

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:03 24 4
gpt4 key购买 nike

我有一组横幅图像,当用户滑动它时可以滑动,并且触摸图像将重定向到某个网站。我想向横幅图像添加自动滚动,但我不太确定该怎么做,有人可以帮我看一下下面的代码吗?

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);
}
}

最佳答案

您可以通过使用 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();
}

关于java - 使横幅图像自动滚动 - Android-Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45131768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com