使用ViewPager / PagerAdapter改变一个字符串 [英] Use a ViewPager / PagerAdapter to change a string

查看:251
本文介绍了使用ViewPager / PagerAdapter改变一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON请求,获得来自YouTube的响应:

  @覆盖
   保护无效doInBackground(虚空......为arg0){
       尝试{         HttpClient的客户端=新DefaultHttpClient();         HttpUriRequest请求=新HTTPGET(https://gdata.youtube.com/feeds/api/videos?author=+ PLAYLIST +与& V = 2及ALT = jsonc);

我想用一个滑动式的页脚图像( pagerAdapter )要改变字符串PLAYLIST的值。我主要关注的是试图找出如何使用 PagerAdapter 如下图所示设置如下:

 字符串PLAYLIST =VEVO;
字符串PLAYLIST =TheMozARTGROUP;
字符串PLAYLIST =TimMcGrawVEVO;
字符串PLAYLIST =TiestoVEVO;
字符串PLAYLIST =EminemVEVO;

在此之后立即播放列表的值由 PagerAdapter 采用PLAYLIST的新值创建播放列表:

 新GetYouTubeUserVideosTask(ResponseHandler所,PLAYLIST).execute();

我已经创建了一个包含我想使用的值一个新的字符串数组。我的问题,现在该了解所有的是:我怎么可以修改下面的源,设置使用数组值中的一个播放列表的价值并执行新的播放列表? (目前我的源代码编译,但是当我刷卡 PagerAdapter - 什么也没有发生)

 新GetYouTubeUserVideosTask(ResponseHandler所,PLAYLIST).execute();

目前非功能寻呼机适配器:

 私有类ImagePagerAdapter扩展PagerAdapter {
        公共ImagePagerAdapter(活动行为,INT [] mImages,
                的String [] stringArra){
            imageArray = mImages;
            活性=行为;
            字符串数组= stringArra;
        }        //这是你的构造
        公共ImagePagerAdapter(){
            超();
            // setOnPageChangeListener(mPageChangeListener);
        }        私人诠释[] = mImages新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};        私有String []字符串数组=新的String [] {VEVO
                TheMozARTGROUP,TimMcGrawVEVO,TiestoVEVO,
                EminemVEVO};        @覆盖
        公众诠释的getCount(){
            返回mImages.length;
        }        @覆盖
        公共布尔isViewFromObject(查看视图,Object对象){
            返回查看==((ImageView的)对象);
        }        @覆盖
        公共对象instantiateItem(ViewGroup中的容器,INT位置){
            上下文的背景下= Home.this;
            ImageView的ImageView的=新ImageView的(上下文);            imageView.setImageResource(mImages [位置]);
            ((ViewPager)容器).addView(ImageView的,0);
            返回ImageView的;
        }        @覆盖
        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
            ((ViewPager)容器).removeView((ImageView的)对象);
        }        私人最终ViewPager.SimpleOnPageChangeListener mPageChangeListener =新ViewPager.SimpleOnPageChangeListener(){            @覆盖
            公共无效使用onPageSelected(最终诠释位置){
                onTabChanged(mPager.getAdapter(),mCurrentTabPosition,位置);
                mCurrentTabPosition =位置;            }
        };        保护无效onTabChanged(最终PagerAdapter适配器,
                最终诠释oldPosition,最终诠释在newPosition){
            如果刷卡是从左到右,或者从右到左//计算器
            如果(oldPosition>在newPosition){
                // 左到右
            }其他{
                // 右到左                查看VG = findViewById(R.layout.home);
                vg.invalidate();
            }
            最后ViewPager viewPager =(ViewPager)findViewById(R.id.view_pager);            viewPager.setOnPageChangeListener(新OnPageChangeListener(){                INT oldPos = viewPager.getCurrentItem();                @覆盖
                公共无效onPageScrolled(INT位置,浮ARG1,ARG2 INT){                    如果(位置> oldPos){
                        //向右移动                    }否则如果(仓位< oldPos){
                        //向左移动                        查看VG = findViewById(R.layout.home);
                        vg.invalidate();
                    }
                }                @覆盖
                公共无效onPageScrollStateChanged(INT为arg0){
                    // TODO自动生成方法存根                }                @覆盖
                公共无效使用onPageSelected(INT为arg0){
                    // TODO自动生成方法存根                }            });        }
    }
}

截图:

其他信息:


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


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



解决方案

尝试学习这回答然后改变你的code效仿的例子更新updateBananaslistArray在调用的notifyDataSetChanged()。通知呼叫是更新您正在使用的适配器查看信号。

在您的code时,getYouTube ...需要更新和数组对象适配器有一个参考。

您应该能够通过在code实现从样品中相同的连接得到它。

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

解决方案

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.

这篇关于使用ViewPager / PagerAdapter改变一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆