0

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

主要活动

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;
    }
}
4

2 回答 2

3

首先,当您刷卡时,您必须弄清楚您当前的页面。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注册你的听众onCreate()

   pagelistener=new PageListener();
pager.setOnPageChangeListener(pagelistener);
于 2013-05-29T07:04:18.777 回答
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修饰符。

于 2013-05-29T20:40:33.270 回答