예제 #1
0
 @Override
 protected void onResume() {
   super.onResume();
   if (viewPager != null) {
     viewPager.startAutoScroll();
   }
   if (container != null) {
     container.smoothScrollTo(0, 0);
   }
   HttpUtils.getpertemcount(
       new HttpErrorHandler() {
         @Override
         public void onRecevieSuccess(JSONObject json) {
           String jishi = json.getJSONObject(UrlContants.jsonData).getString("jishi");
           String yongjiu = json.getJSONObject(UrlContants.jsonData).getString("yongjiu");
           if (CommonUtils.CheckLogin() && Integer.valueOf(StringUtil.toString(jishi, "0")) > 0) {
             badgeView1.show();
             badgeView1.setText(jishi);
           } else {
             badgeView1.hide();
           }
           if (CommonUtils.CheckLogin()
               && Integer.valueOf(StringUtil.toString(yongjiu, "0")) > 0) {
             badgeView2.show();
             badgeView2.setText(yongjiu);
           } else {
             badgeView2.hide();
           }
         }
       });
 }
  @Override
  protected void onResume() {
    super.onResume();

    if (null != viewPager) {
      viewPager.startAutoScroll();
    }
  }
예제 #3
0
  /** 加载页面 */
  private void initView() {
    /*头部标题*/
    myCommonTitle = (MyCommonTitle) findViewById(R.id.aci_mytitle);
    myCommonTitle.setTitle("喜刷刷");
    myCommonTitle.setBackBtnVisible(false);
    myCommonTitle.setLisener(null, this);

    container = (ScrollView) findViewById(R.id.container);

    viewPager = (AutoScrollViewPager) findViewById(R.id.index_slider); // 轮播图
    tv_news_more = (LinearLayout) findViewById(R.id.tv_index_gift); // 新闻更多
    index_list = (AutoListView) findViewById(R.id.index_list); // 新闻资讯
    index_list.setOnItemClickListener(this);
    index_gift = (LinearLayout) findViewById(R.id.index_gift); // 中间三大模块(即时,永久,兴趣)

    index_image1 = (ImageView) findViewById(R.id.index_image1); // 即时红包
    index_image2 = (ImageView) findViewById(R.id.index_image2); // 永久红包
    index_image3 = (ImageView) findViewById(R.id.index_image3); // 兴趣标签

    LayoutParams pageParms = viewPager.getLayoutParams();
    pageParms.width = Tools.M_SCREEN_WIDTH;
    pageParms.height = Tools.M_SCREEN_WIDTH * 2 / 5;

    LayoutParams imagelayout = index_gift.getLayoutParams();
    imagelayout.width = Tools.M_SCREEN_WIDTH;
    imagelayout.height = Tools.M_SCREEN_WIDTH * 11 / 30;
    badgeView1 = new BadgeView(IndexActivity.this, index_image1);
    badgeView1.setText("0");
    // badgeView1.hide();
    badgeView2 = new BadgeView(IndexActivity.this, index_image2);
    badgeView2.setText("0");
    // badgeView2.hide();

    viewPager.setInterval(2000);
    viewPager.startAutoScroll();

    viewPager.setOnPageChangeListener(
        new OnPageChangeListener() {
          public void onPageSelected(int position) {
            // 回调view
            uihandler.obtainMessage(0, position).sendToTarget();
          }

          public void onPageScrolled(int position, float positionOffset, int positionOffsetPx) {}

          public void onPageScrollStateChanged(int position) {}
        });

    setListener(index_image1, index_image2, index_image3, tv_news_more);
  }
  /**
   *
   *
   * <ul>
   *   if stopScrollWhenTouch is true
   *   <li>if event is down, stop auto scroll.
   *   <li>if event is up, start auto scroll again.
   * </ul>
   */
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);

    if (stopScrollWhenTouch) {
      if ((action == MotionEvent.ACTION_DOWN) && isAutoScroll) {
        isStopByTouch = true;
        stopAutoScroll();
      } else if (ev.getAction() == MotionEvent.ACTION_UP && isStopByTouch) {
        startAutoScroll();
      }
    }

    if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT
        || slideBorderMode == SLIDE_BORDER_MODE_CYCLE) {
      touchX = ev.getX();
      if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        downX = touchX;
      }
      int currentItem = getCurrentItem();
      PagerAdapter adapter = getAdapter();
      int pageCount = adapter == null ? 0 : adapter.getCount();
      /**
       * current index is first one and slide to right or current index is last one and slide to
       * left.<br>
       * if slide border mode is to parent, then requestDisallowInterceptTouchEvent false.<br>
       * else scroll to last one when current item is first one, scroll to first one when current
       * item is last one.
       */
      if ((currentItem == 0 && downX <= touchX)
          || (currentItem == pageCount - 1 && downX >= touchX)) {
        if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT) {
          getParent().requestDisallowInterceptTouchEvent(false);
        } else {
          if (pageCount > 1) {
            setCurrentItem(pageCount - currentItem - 1, isBorderAnimation);
          }
          getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
      }
    }
    getParent().requestDisallowInterceptTouchEvent(true);

    return super.dispatchTouchEvent(ev);
  }
  private void initViewPager() {
    // indicator
    indicatorLayout = (LinearLayout) this.findViewById(R.id.indicatorLayout);
    indicatorLayout.removeAllViews();

    indicatorImageViews = new ImageView[imageURLList.size()];
    for (int i = 0; i < imageURLList.size(); i++) {
      ImageView imageView = new ImageView(this);
      imageView.setLayoutParams(new LinearLayout.LayoutParams(10, 10));
      if (i == 0) {
        imageView.setBackgroundResource(R.drawable.page_indicator_focused);
      } else {
        imageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
      }

      indicatorImageViews[i] = imageView;

      LinearLayout.LayoutParams layoutParams =
          new LinearLayout.LayoutParams(
              new ViewGroup.LayoutParams(
                  LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
      layoutParams.leftMargin = 10;
      layoutParams.rightMargin = 10;
      indicatorLayout.addView(indicatorImageViews[i], layoutParams);
    }

    // http://www.trinea.cn/android/auto-scroll-view-pager/
    // ViewPager
    viewPager = (AutoScrollViewPager) this.findViewById(R.id.viewPager);
    viewPager.setInterval(3000);
    viewPager.setCycle(true);
    viewPager.setAutoScrollDurationFactor(7.0);
    viewPager.setSlideBorderMode(AutoScrollViewPager.SLIDE_BORDER_MODE_CYCLE);
    viewPager.setStopScrollWhenTouch(false);
    viewPagerAdapter = new ImagePagerAdapter(this, imageURLList);

    viewPager.setOnPageChangeListener(
        new ViewPager.OnPageChangeListener() {

          @Override
          public void onPageScrollStateChanged(int arg0) {}

          @Override
          public void onPageScrolled(int arg0, float arg1, int arg2) {}

          @Override
          public void onPageSelected(int index) {
            int position = index % imageURLList.size();
            for (int i = 0; i < imageURLList.size(); i++) {
              if (i == position) {
                indicatorImageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
              } else {
                indicatorImageViews[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
              }
            }
          }
        });
    // viewPagerAdapter.setInfiniteLoop(true);
    viewPager.setAdapter(viewPagerAdapter);
    viewPager.startAutoScroll();

    final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
    viewPager.setOnTouchListener(
        new View.OnTouchListener() {
          public boolean onTouch(View v, MotionEvent event) {
            tapGestureDetector.onTouchEvent(event);
            return false;
          }
        });
  }