Ejemplo n.º 1
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);
  }
  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;
          }
        });
  }