使用寻呼机适配器保存和恢复View Pager的状态

时间:2014-08-13 16:37:05

标签: android listview android-viewpager parcelable

我正在使用带有寻呼机适配器的视图寻呼机。抓住的是我不能使用Fragments。所以一切都是使用views完成的。这是代码

private class MyPagerAdapter extends PagerAdapter {
    private View temp;
    public MyPagerAdapter() {
    }

    @Override
    public int getCount() {
        return 3;

    }
    public void get(ViewParent viewParent){
        temp=(View) viewParent;
    }
    @Override
    public int getItemPosition(Object object) {
        if(object.equals(temp)){
            Log.d("Serve","The gridview is about to be removed");
            return POSITION_NONE;

        }
        else {
            Log.d("Serve","The gridview is not to be removed");
            return POSITION_UNCHANGED;
        }
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("SERVE","3");
       View view=null;

        switch (position) {
        case 0:
            if(albums==-1){
                view = inflater.inflate(R.layout.grid, null);
                gv=(GridView)view.findViewById(R.id.grid_view);
                gridAdapter=new GridAdapter(Serve.this,Artists.songList,0);     
                gv.setAdapter(gridAdapter);
                gv.setOnItemClickListener(new OnItemClickListener(){

                    @Override
                    public void onItemClick(AdapterView<?> v, View arg1,
                            int pos, long arg3) {
                        get(v.getParent());
                        setAlbum(pos);
                        notifyDataSetChanged();

                    }
                });
            }
            else {
                view = inflater.inflate(R.layout.activity_title, null);
                lv=(ListView)view.findViewById(R.id.music_list);
                //lv.setFadingEdgeLength(0);
                songAdapter=new SongAdapter(Serve.this,Artists.songList,0,albums);
                /*SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(songAdapter);

                // Assign the ListView to the AnimationAdapter and vice versa
                swingRightInAnimationAdapter.setAbsListView(lv);
                lv.setAdapter(swingRightInAnimationAdapter);*/
                lv.setAdapter(songAdapter);
                lv.setOnItemClickListener(new OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        for(int i=0;i<Artists.songList.size();i++){
                            if(Artists.songList.get(i).getTitle().equals
                                    (((SongAdapter.getAlbumSongs().get(position))))){
                                songPicked(i);
                                break;
                            }
                        }
                        ChatHeadService.updateHead();
                    }
                });
            }
            break;

        //Removed case 1 and case 2 because they are similar to case 0.

        Log.d("SERVE","The view is about to be initialised");
        /*View view = inflater.inflate(resId, null);
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < names.length; ++i) {
          list.add(names[i]+i+1);
        }*/
        //lv.setAdapter(songAdapter);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }
    @Override
    public CharSequence getPageTitle(int pos) {
        if(pos==0) return "Albums";
        else if(pos==1) return "Artists";
        else if(pos==2) return "Songs";
        else return "Genre";
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
         ((ViewPager) collection).removeView((View) view);
    }


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

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

我希望了解跨实例保存寻呼机状态的最佳方法。 一个可分割的对象在性能上比单纯使用像scrollTo(x,y)这样的函数更快,其中x和y可以使用共享首选项存储。

0 个答案:

没有答案