viewpager onPageSelected没有执行

pianpian
  • 31

源码如下:

package com.yangyu.myguideview04;

import java.util.ArrayList;
import java.util.List;

import com.yangyu.myguideview04.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener,OnPageChangeListener{
    
    private RelativeLayout changyonggongneng;
    private RelativeLayout touzilicai;
    private RelativeLayout yidongshenghuo;
    private RelativeLayout gengduo;
    
    private ImageView menu_item_iv_cygn;
    private ImageView menu_item_iv_tzlc;
    private ImageView menu_item_iv_ydsh;
    private ImageView menu_item_iv_gd;
    
    private TextView menu_item_txt_cygn;
    private TextView menu_item_txt_tzlc;
    private TextView menu_item_txt_ydsh;
    private TextView menu_item_txt_gd;
    
    private ViewPager viewPager;
    
    private ContentAdapter adapter;
    
    private List<View> views;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initEvent() {
        // TODO Auto-generated method stub
        changyonggongneng.setOnClickListener(this);
        touzilicai.setOnClickListener(this);
        yidongshenghuo.setOnClickListener(this);
        gengduo.setOnClickListener(this);
        
        viewPager.setOnClickListener(this);
    }

    private void initView() {
        // TODO Auto-generated method stub
        this.changyonggongneng=(RelativeLayout)findViewById(R.id.changyonggongneng);
        this.touzilicai=(RelativeLayout)findViewById(R.id.touzilicai);
        this.yidongshenghuo=(RelativeLayout)findViewById(R.id.yidongshenghuo);
        this.gengduo=(RelativeLayout)findViewById(R.id.gengduo);
        
        
        this.menu_item_iv_cygn=(ImageView)findViewById(R.id.menu_item_iv_cygn);
        this.menu_item_iv_tzlc=(ImageView)findViewById(R.id.menu_item_iv_tzlc);
        this.menu_item_iv_ydsh=(ImageView)findViewById(R.id.menu_item_iv_ydsh);
        this.menu_item_iv_gd=(ImageView)findViewById(R.id.menu_item_iv_gd);
        
        this.menu_item_txt_cygn=(TextView)findViewById(R.id.menu_item_txt_cygn);
        this.menu_item_txt_tzlc=(TextView)findViewById(R.id.menu_item_txt_tzlc);
        this.menu_item_txt_ydsh=(TextView)findViewById(R.id.menu_item_txt_ydsh);
        this.menu_item_txt_gd=(TextView)findViewById(R.id.menu_item_txt_gd);
        
        this.viewPager=(ViewPager)findViewById(R.id.vp_content);
        
        View menu_01=View.inflate(MainActivity.this, R.layout.menu_01, null);
        View menu_02=View.inflate(MainActivity.this, R.layout.menu_02, null);
        View menu_03=View.inflate(MainActivity.this, R.layout.menu_03, null);
        View menu_04=View.inflate(MainActivity.this, R.layout.menu_04, null);
        
        views=new ArrayList<View>();
        views.add(menu_01);
        views.add(menu_02);
        views.add(menu_03);
        views.add(menu_04);
        
        
        this.adapter=new ContentAdapter(views);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub

        restartBotton();        
        switch (arg0) {
        case 0:
            menu_item_iv_cygn.setImageResource(R.drawable.common_btn_press);
            menu_item_txt_cygn.setTextColor(0xff1B940A);
            System.out.print("执行l第一个");
            break;
        case 1:
            menu_item_iv_tzlc.setImageResource(R.drawable.investment_btn_press);
            menu_item_txt_tzlc.setTextColor(0xff1B940A);
            System.out.print("执行l第二个");
            break;
        case 2:
            menu_item_iv_ydsh.setImageResource(R.drawable.mobilelife_btn_press);
            menu_item_txt_ydsh.setTextColor(0xff1B940A);
            System.out.print("执行l第三个");
            break;
        case 3:
            menu_item_iv_gd.setImageResource(R.drawable.more_btn_press);
            menu_item_txt_gd.setTextColor(0xff1B940A);
            System.out.print("执行l第四个");
            break;

        default:
            break;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        restartBotton();
        switch (v.getId()) {
        case R.id.changyonggongneng:
            menu_item_iv_cygn.setImageResource(R.drawable.common_btn_press);
            menu_item_txt_cygn.setTextColor(0xff1B940A);
            viewPager.setCurrentItem(0);
            break;
        case R.id.touzilicai:
            menu_item_iv_tzlc.setImageResource(R.drawable.investment_btn_press);
            menu_item_txt_tzlc.setTextColor(0xff1B940A);
            viewPager.setCurrentItem(1);
            break;
        case R.id.yidongshenghuo:
            menu_item_iv_ydsh.setImageResource(R.drawable.mobilelife_btn_press);
            menu_item_txt_ydsh.setTextColor(0xff1B940A);
            viewPager.setCurrentItem(2);
            break;
        case R.id.gengduo:
            menu_item_iv_gd.setImageResource(R.drawable.more_btn_press);
            menu_item_txt_gd.setTextColor(0xff1B940A);
            viewPager.setCurrentItem(3);
            break;

        default:
            break;
        }
    }
    private void restartBotton() {
        // TODO Auto-generated method stub
        menu_item_iv_cygn.setImageResource(R.drawable.common_btn_normal);
        menu_item_iv_tzlc.setImageResource(R.drawable.investment_btn_normal);
        menu_item_iv_ydsh.setImageResource(R.drawable.mobilelife_btn_normal);
        menu_item_iv_gd.setImageResource(R.drawable.more_btn_normal);
        
        menu_item_txt_cygn.setTextColor(0xffffffff);
        menu_item_txt_tzlc.setTextColor(0xffffffff);
        menu_item_txt_ydsh.setTextColor(0xffffffff);
        menu_item_txt_gd.setTextColor(0xffffffff);
        
    }
    
}

当我滑动页面是,底部的菜单按钮并没有随页面滑动而切换。

回复
阅读 6.1k
1 个回答
✓ 已被采纳
this.viewPager.setOnPageChangeListener(this);
宣传栏