ViewPager无法实现Case Statement或stringArray

时间:2013-12-19 16:46:55

标签: java android android-viewpager switch-statement case

我有一个viewPager,我想结合case语句或stringArray(两者都在我的源代码中显示)但是我似乎无法让它们一起工作。目前我正在尝试显示4个带有不同消息的吐司用于测试目的,但是当我滑动viewPager时我似乎无法看到它们。

private int mCurrentTabPosition = NO_CURRENT_POSITION;
private static final int NO_CURRENT_POSITION = -1;
int imageArray[];
private OnPageChangeListener mPageChangeListener;
ImagePagerAdapter adapter = new ImagePagerAdapter();
private String[] stringArray = new String[] { "Value1", "Value2",
        "Value3", "Value4", "Value5‎" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    final ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
    adapter.notifyDataSetChanged();
    mPager.setAdapter(adapter);

    mPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int pos) {

            String playlist1 = stringArray[pos];
            if (playlist != null) {
                new GetYouTubeUserVideosTask(responseHandler, playlist1)
                        .execute();

            }
        }

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

        }

        @Override
        public void onPageScrollStateChanged(int pos) {

        }
    });

    mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
            int focusedPage = 0;
            focusedPage = position;

            switch (focusedPage) {
            case 0: { Toast.makeText(Home.this, "Case 0", Toast.LENGTH_LONG).show();
                break;
            }
            case 1: { Toast.makeText(Home.this, "Case 1", Toast.LENGTH_LONG).show();
                break;
            }
            case 2: { Toast.makeText(Home.this, "Case 2", Toast.LENGTH_LONG).show();
                break;
            }
            case 3: { Toast.makeText(Home.this, "Case 3", Toast.LENGTH_LONG).show();
                break;
            }
            }

        }

        private void onTabChanged(PagerAdapter adapter,
                int mCurrentTabPosition, int position) {

            Log.d("PK", "Tab changed");
            // TODO Auto-generated method stub

        }
    };
}

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    if (position == 1) {
        Toast.makeText(Home.this, "Item 1", Toast.LENGTH_LONG).show();
    } else if (position == 2) {
        Toast.makeText(Home.this, "Item 2", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(Home.this, "Item 3", Toast.LENGTH_LONG).show();
    }

    // update selected item and title, then close the drawer
    drawerListView.setItemChecked(position, true);
    drawerLayout.closeDrawer(drawerListView);
}


private class ImagePagerAdapter extends PagerAdapter implements
        ViewPager.OnPageChangeListener {
    public ImagePagerAdapter(Activity act, int[] mImages,
            String[] stringArra) {
        imageArray = mImages;
        activity = act;
        setStringArray(stringArra);
    }

    public ImagePagerAdapter() {
        super();

    }

    private int[] mImages = new int[] { R.image1,
            R.drawable.image2, R.drawable.image3,
            R.drawable.image4, R.drawable.image5 };

    @Override
    public int getCount() {
        return mImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }



    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }



    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
        Log.d("pk", "onPageScrollStateChanged");
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
        Log.d("pk", "onPageScrolled");
    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        Log.d("pk", "onPageSelected");
    }

    public String[] getStringArray() {
        return stringArray;
    }

    public void setStringArray(String[] stringArray) {
        // this.stringArray = stringArray;
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用以下

mPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int pos) {

            switch (pos) {
        case 0: Toast.makeText(Home.this, "Case 0", Toast.LENGTH_LONG).show();
            break;

        case 1:  Toast.makeText(Home.this, "Case 1", Toast.LENGTH_LONG).show();
            break;

        case 2:  Toast.makeText(Home.this, "Case 2", Toast.LENGTH_LONG).show();
            break;

        case 3:  Toast.makeText(Home.this, "Case 3", Toast.LENGTH_LONG).show();
            break;

        }
            String playlist1 = stringArray[pos];
            if (playlist != null) {
                new GetYouTubeUserVideosTask(responseHandler, playlist1)
                        .execute();

            }
        }

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

        }

        @Override
        public void onPageScrollStateChanged(int pos) {

        }
    });