ViewPager / PagerAdapter:更改字符串的值

时间:2013-12-11 18:52:22

标签: java android arrays android-viewpager android-pageradapter

我有一个youtube播放列表:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        final ActionBar actionBar = getActionBar();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(adapter);
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        drawerListViewItems = getResources().getStringArray(R.array.items);
        drawerListView = (ListView) findViewById(R.id.left_drawer);
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                drawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description */
                R.string.drawer_close /* "close drawer" description */
                );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
        listView = (VideosListView) findViewById(R.id.videosListView);
        listView.setOnVideoClickListener(this);
        new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();

    }

...

我有一个ViewPager / PagerAdapter:

    private class ImagePagerAdapter extends PagerAdapter {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        public ImagePagerAdapter() {
            super();
            // setOnPageChangeListener(mPageChangeListener);
        }

        private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn, R.drawable.country_up_btn,
                R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                R.drawable.island_up_btn, R.drawable.latin_up_btn,
                R.drawable.pop_up_btn, R.drawable.samba_up_btn };

        private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
        "EminemVEVO‎" };

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

        private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

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

            }
        };

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            if (convertView == null) {
                convertView = LayoutInflater.from(context)
                  .inflate(R.layout.home, parent, false);
            }

            ImageView imageView = new ImageView(context);
            ImageView imageView = (ImageView)vi.findViewById(R.drawable.selstation_up_btn);
            imageView.setImageResource(imageView.getImage());

            return convertView;
        }

        protected void onTabChanged(final PagerAdapter adapter,
                final int oldPosition, final int newPosition) {
            // Calc if swipe was left to right, or right to left
            if (oldPosition > newPosition) {
                // left to right
            } else {
                // right to left

                View vg = findViewById(R.layout.home);
                vg.invalidate();
            }
            final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

            viewPager.setOnPageChangeListener(new OnPageChangeListener() {

                int oldPos = viewPager.getCurrentItem();

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

                    if (position > oldPos) {
                        // Moving to the right

                    } else if (position < oldPos) {
                        // Moving to the Left

                        View vg = findViewById(R.layout.home);
                        vg.invalidate();
                    }
                }

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

                }

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

                }

            });

        }
    }
    }

截图:

enter image description here

问题:

如何连接两者 - 所以当我滑动ViewPager / PagerAdapter时:它会改变以下值:String PLAYLIST

我需要根据ViewPager / PagerAdapter选项更改它的值。

实施例

if R.drawable.classical_up_btn {

String PLAYLIST = "TheMozARTGROUP‎";

}

 if R.drawable.country_up_btn {

   String PLAYLIST = "TimMcGrawVEVO‎";
}

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您可以在onPageChangeListener中更改类级别变量,播放列表。 onPageScrolled中的int position是第一个显示项的索引。

作为旁注,所有大写应保留为常量。可以在代码中更改的任何变量都应以小写字母开头。