通过viewpager更改片段页面时更改图标

时间:2013-05-28 21:24:41

标签: android android-fragments android-viewpager imagebutton viewpagerindicator

我正在使用带有5个片段的viewpager来启用水平滚动。我已经放置了一个与每个页面对应的图标,以便用户可以知道他在哪个页面上。我还编程说如果按下图标,页面的相应图标会突出显示。 但是如果按下并且用户在该页面上,则图标会突出显示。但是,如果我滑到另一个页面,我怎么能更改图标?

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /** Getting a reference to the ViewPager defined the layout file */
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);
        PageListener pagelistener = new PageListener();
        pager.setOnPageChangeListener(pagelistener);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

        pager.setPageTransformer(true, new ZoomOutPageTransformer());

        final ImageButton vpOne = (ImageButton) findViewById(R.id.vpOne);
        final ImageButton vpTwo = (ImageButton) findViewById(R.id.vpTwo);
        final ImageButton vpThree = (ImageButton) findViewById(R.id.vpThree);
        final ImageButton vpFour = (ImageButton) findViewById(R.id.vpFour);
        final ImageButton vpFive = (ImageButton) findViewById(R.id.vpFive);
        final ImageButton vpSix = (ImageButton) findViewById(R.id.vpSix);

        OnClickListener One = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(0, true);
                vpOne.setImageResource(R.drawable.vp_One);
                vpTwo.setImageResource(R.drawable.vp_Two_bw);
                vpThree.setImageResource(R.drawable.vp_Three_bw);
                vpFour.setImageResource(R.drawable.vp_Four_bw);
                vpFive.setImageResource(R.drawable.vp_Five_bw);
                vpSix.setImageResource(R.drawable.vp_Six_bw);
            }
        };

        OnClickListener Two = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(1, true);
                vpOne.setImageResource(R.drawable.vp_One_bw);
                vpTwo.setImageResource(R.drawable.vp_Two);
                vpThree.setImageResource(R.drawable.vp_Three_bw);
                vpFour.setImageResource(R.drawable.vp_Four_bw);
                vpFive.setImageResource(R.drawable.vp_Five_bw);
                vpSix.setImageResource(R.drawable.vp_Six_bw);
            }
        };

        OnClickListener Three = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(2, true);
                vpOne.setImageResource(R.drawable.vp_One_bw);
                vpTwo.setImageResource(R.drawable.vp_Two_bw);
                vpThree.setImageResource(R.drawable.vp_Three);
                vpFour.setImageResource(R.drawable.vp_Four_bw);
                vpFive.setImageResource(R.drawable.vp_Five_bw);
                vpSix.setImageResource(R.drawable.vp_Six_bw);
            }
        };

        OnClickListener Four = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(3, true);
                vpOne.setImageResource(R.drawable.vp_One_bw);
                vpTwo.setImageResource(R.drawable.vp_Two_bw);
                vpThree.setImageResource(R.drawable.vp_Three_bw);
                vpFour.setImageResource(R.drawable.vp_Four);
                vpFive.setImageResource(R.drawable.vp_Five_bw);
                vpSix.setImageResource(R.drawable.vp_Six_bw);
            }

        };

        OnClickListener Five = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(4, true);
                vpOne.setImageResource(R.drawable.vp_One_bw);
                vpTwo.setImageResource(R.drawable.vp_Two_bw);
                vpThree.setImageResource(R.drawable.vp_Three_bw);
                vpFour.setImageResource(R.drawable.vp_Four_bw);
                vpFive.setImageResource(R.drawable.vp_Five);
                vpSix.setImageResource(R.drawable.vp_Six_bw);
            }
        };

        OnClickListener Six = new OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(5, true);
                vpOne.setImageResource(R.drawable.vp_One_bw);
                vpTwo.setImageResource(R.drawable.vp_Two_bw);
                vpThree.setImageResource(R.drawable.vp_Three_bw);
                vpFour.setImageResource(R.drawable.vp_Four_bw);
                vpFive.setImageResource(R.drawable.vp_Five_bw);
                vpSix.setImageResource(R.drawable.vp_Six);
            }
        };

        vpOne.setOnClickListener(One);
        vpTwo.setOnClickListener(Two);
        vpThree.setOnClickListener(Three);
        vpFour.setOnClickListener(Four);
        vpFive.setOnClickListener(Five);
        vpSix.setOnClickListener(Six);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

class PageListener extends SimpleOnPageChangeListener {

    final ImageButton vpOne = (ImageButton) findViewById(R.id.vpOne);
    final ImageButton vpTwo = (ImageButton) findViewById(R.id.vpTwo);
    final ImageButton vpThree = (ImageButton) findViewById(R.id.vpThree);
    final ImageButton vpFour = (ImageButton) findViewById(R.id.vpFour);
    final ImageButton vpFive = (ImageButton) findViewById(R.id.vpFive);
    final ImageButton vpSix = (ImageButton) findViewById(R.id.vpSix);

    public void onPageSelected(int position) {
        int noOfPages = 5;
        int currentPage = position;
        if (currentPage % noOfPages == 0) {
            vpOne.setImageResource(R.drawable.vp_One);
            vpTwo.setImageResource(R.drawable.vp_Two_bw);
            vpThree.setImageResource(R.drawable.vp_Three_bw);
            vpFour.setImageResource(R.drawable.vp_Four_bw);
            vpFive.setImageResource(R.drawable.vp_Five_bw);
            vpSix.setImageResource(R.drawable.vp_Six_bw);
        }

        else if (currentPage % noOfPages == 1) {
            // add like the above
        }

        else if (currentPage % noOfPages == 2) {
            // add like the above
        }

        else if (currentPage % noOfPages == 3) {
            // add like the above
        }

        else if (currentPage % noOfPages == 4) {
            // add like the above
        }
    }
}

MyFragmentPagerAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 6;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        switch(arg0){

        case 0:

            return new OneFragment();           

        case 1:
            return new TwoFragment();

        case 2:
            return new ThreeFragment();

        case 3:
            return new FourFragment();

        case 4:
            return new FiveFragment();

        case 5:
            return new SixFragment();           

        default:
            return null;

        }       
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }    
}

我在每个片段中尝试了以下内容,但没有奏效:

片段

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class OneFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             View v = inflater.inflate(R.layout.one, container, false);

             ImageButton vpOne = (ImageButton) v.findViewById(R.id.vpOne);
             ImageButton vpTwo = (ImageButton) v.findViewById(R.id.vpTwo);
             ImageButton vpThree = (ImageButton) v.findViewById(R.id.vpThree);
             ImageButton vpFour = (ImageButton) v.findViewById(R.id.vpFour);
             ImageButton vpFive = (ImageButton) v.findViewById(R.id.vpFive);
             ImageButton vpSix = (ImageButton) v.findViewById(R.id.vpSix);

             vpOne.setImageResource(R.drawable.vp_One);
             vpTwo.setImageResource(R.drawable.vp_Two_bw);
             vpThree.setImageResource(R.drawable.vp_Three_bw);
             vpFour.setImageResource(R.drawable.vp_Four_bw);
             vpFive.setImageResource(R.drawable.vp_Five_bw);
             vpSix.setImageResource(R.drawable.vp_Six_bw);

             return v;
    }
}

2 个答案:

答案 0 :(得分:2)

首先,你需要在刷卡时找出当前页面。 为此,您必须通过实现实现页面侦听器 SimpleOnPageChangeListener作为MainActivity内部类的内部类应该像这样

private static class PageListener extends SimpleOnPageChangeListener{
        public void onPageSelected(int position) {
            int noOfPages =5;
            currentPage = position;
            if(currentPage % noOfPages == 0){
        vpOne.setImageResource(R.drawable.vp_One);
            vpTwo.setImageResource(R.drawable.vp_Two_bw);
            vpThree.setImageResource(R.drawable.vp_Three_bw);
            vpFour.setImageResource(R.drawable.vp_Four_bw);
            vpFive.setImageResource(R.drawable.vp_Five_bw);
            vpSix.setImageResource(R.drawable.vp_Six_bw);
            }

            else if(currentPage % noOfPages == 1){
                //add like the above
            }

            else if(currentPage % noOfPages == 2){
            //add like the above
            }

            else if(currentPage % noOfPages == 3){
                //add like the above
            }

            else if(currentPage % noOfPages == 4){
                //add like the above
            }
        }
    }

MainActivity的{​​{1}}注册您的听众,内部就是这样做

onCreate()

答案 1 :(得分:1)

viewPagerSpecialist的答案应该解决您的初始问题,但您的另一个问题是尝试访问新创建的内部类中的Views。您需要在Views之外声明onCreate(),以便您的内部类可以访问它们,如

public class MainActivity extends FragmentActivity {

    ImageButton vpOne;  //declare them here
    ImageButton vpTwo;

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

    vpOne = (ImageButton) findViewById(R.id.vpOne);  //initialize them here
    vpTwo = (ImageButton) findViewById(R.id.vpTwo);

将他们从新班级中取出。现在,您可以在内部类中访问它们。请注意,我还删除了final修饰符。