1
我有一組橫幅圖像,可以滑動時,用戶將其滑動並觸摸圖像將重定向到某個網站。 我想添加自動滾動到橫幅圖像,但我不太清楚如何去做,任何人都可以幫我看看下面的代碼嗎?製作橫幅圖像AutoScroll - Android-Java
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);
}
}