2017-07-16 80 views
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); 
    } 
} 

回答

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