Use a ViewPager / PagerAdapter to change a string

丶灬走出姿态 提交于 2019-12-12 06:40:11

问题


I have a JSON request which gets a response from YouTube:

@Override
   protected Void doInBackground(Void... arg0) {
       try {

         HttpClient client = new DefaultHttpClient();

         HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?author="+PLAYLIST+"&v=2&alt=jsonc");

I would like to use a swipeable footer image (pagerAdapter) to change the value of the string PLAYLIST. My primary concern is trying to figure out how to use the PagerAdapter shown below to set the following:

String PLAYLIST = "vevo";
String PLAYLIST = "TheMozARTGROUP‎";
String PLAYLIST = "TimMcGrawVEVO‎";
String PLAYLIST = "TiestoVEVO‎";
String PLAYLIST = "EminemVEVO‎";

then immediately after the value of PLAYLIST is set by the PagerAdapter, create a playlist using the new value of PLAYLIST:

new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();

I've created a new string array containing the values I'd like to use. My question now that all that is understood is: how can I modify the source below to set the value of PLAYLIST using one of the array values and execute the new playlist? (Currently my source compiles but when I swipe the PagerAdapter - nothing happens)

new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();

Currently non-functional pager adapter:

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

        // this is your constructor
        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 };

        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.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;

            }
        };

        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

                }

            });

        }
    }
}

Screenshot:

Additional info:

  1. http://developer.android.com/training/animation/screen-slide.html

  2. http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html


回答1:


Try studying this answer and then changing your code to follow example of updating the 'updateBananas' listArray prior to calling 'notifyDataSetChanged()'. The call to notify is the signal to update the View in the adapter you are using.

In your code, the getYouTube... needs to update and array object that the adapter has a reference to.

you should be able to get it by implementing the same connections from the sample in your code.



来源:https://stackoverflow.com/questions/20473297/use-a-viewpager-pageradapter-to-change-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!