ViewPager的使用简单记录

来源:互联网 发布:乐知英语待遇 编辑:程序博客网 时间:2024/05/20 00:35

         之前在通知页中使用了ViewPager, 这次要实现页功能网上有看到ViewPager的两个相近控件ViewFlinger  和ViewFlow ,其中ViewFlinger适用于Acitivity间切换,ViewFlow适用广告banner 的切换,因此这次还是用了ViewPager .

         ViewPager在Surport_v4包里面,他的使用很简单:

1.    布局文件中:

        <android.support.v4.view.ViewPager            android:id="@+id/file_viewpager"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </android.support.v4.view.ViewPager>

2. 重写PagerAdapter

    

    private class GridPageAdapter extends PagerAdapter {List<File> mFiles;private Map<Integer,FilePageView> pages=new HashMap<Integer,FilePageView>();public GridPageAdapter(List<File> files){mFiles=files;}public void setFiles(List<File> files){mFiles=files;notifyDataSetChanged();}        @Override        public int getCount() {                if(mFiles.size()==0){        Log.d("you", "getCount()  mFiles.size()==0 return 1");        return 1;        }                int size=mFiles.size();        int pageCount=(size % PAGE_SIZE==0)? size/PAGE_SIZE : (int)Math.floor(size/PAGE_SIZE)+1;                Log.d("you", "getCount()  pageCount:"+pageCount+" mFiles.size():"+mFiles.size());            return pageCount;        }        @Override        public boolean isViewFromObject(View arg0, Object arg1) {            return arg0 == arg1;        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView(pages.get(position));        //super.destroyItem(container, position, object);        }        @Override        public int getItemPosition(Object object) {            return super.getItemPosition(object);        }        @Override        public CharSequence getPageTitle(int position) {            return "";        }        @Override        public Object instantiateItem(ViewGroup container, int position) {        FilePageView view =(FilePageView) View.inflate(FileExplorerActivity.this, R.layout.file_page_view, null);        pages.put(position, view);        //FileItemView view = (FileItemView) View.inflate(FileExplorerActivity.this, R.layout.file_item_view, null);        //view.getDiskNameView().setText("position"+position);                if(mFiles.size()==0){         Log.d("you", "instantiateItem..... mFiles.size==0 return");         container.addView(view);                 return view;        }                        GridView  gridView=view.getGridView();        int start = position*PAGE_SIZE;        int end=(position+1)*PAGE_SIZE;        if(end>mFiles.size()){        end=mFiles.size();        }        Log.d("you", "instantiateItem..... mFiles.size:"+mFiles.size()+ " start:"+start+ " end:"+end);        List<File> files=mFiles.subList(start, end);        FileGridViewAdapter adapter = new FileGridViewAdapter(FileExplorerActivity.this);        adapter.setDiskDeviceInfos(files);gridView.setAdapter(adapter);        gridView.setOnItemClickListener(new MyOnItemClickListener(gridView, adapter));            container.addView(view);            return view;//mPageLists.get(position);        }    }

3.activity中

  

                mViewPager=(ViewPager) findViewById(R.id.file_viewpager);mGridPagerAdapter=new GridPageAdapter(files);mViewPager.setAdapter(mGridPagerAdapter);


上面三个步就已经用ok了,再有就是加上 指示器了,指示器可以用一个TextView

        <TextView            android:id="@+id/page_indicator_view"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="@android:color/white"            android:textSize="@dimen/disk_name_text_size"            android:layout_alignParentRight="true"            android:layout_alignParentBottom="true"            android:layout_marginRight="@dimen/indicator_view_margin_right"            android:layout_marginBottom="@dimen/indicator_view_margin_bottom" />private class PageChangeListener implements OnPageChangeListener {        @Override        public void onPageScrollStateChanged(int arg0) {}        @Override        public void onPageScrolled(int arg0, float arg1, int arg2) {}        @Override        public void onPageSelected(int position) {        Log.d("you", "onPageSelected.....position:"+position);mPageIndicatorView.setText(getResources().getString(R.string.page_indicator, (position+1)+"/"+mGridPagerAdapter.getCount()));        }    }     mPageChangeListener=new PageChangeListener();     ViewPager.setOnPageChangeListener(mPageChangeListener);



0 0
原创粉丝点击