使用ViewPager和GridView配合,實現GridView橫向水平滑動的效果。


實現的原理其實很簡單,就是利用Viewpager加入靜態的GridView,GridView是寫死的。
首先要引入android-support-v4.jar這個包,這個包是用來支持ViewPager這個組件的。
 
然後在主類的Oncreate方法裏:

LayoutInflater   mInflater = getLayoutInflater();

   //LayoutInflater可以將一個xml佈局文件轉化爲View 類型
      View   view_01 = mInflater.inflate(R.layout.layout1null);
      View    view_02 = mInflater.inflate(R.layout.layout2null);
        private List<View> mViewList = new ArrayList<View>();// 存放待滑動的view
        mViewList.clear();//清空老的pager內容後 再加入新的。
        mViewList.add(view_01);

        mViewList.add(view_02);

        GridView gridview = (GridView) view_01.findViewById(R.id.gridview);
        GridView gridview2 = (GridView) view_02.findViewById(R.id.gridview2);
            (通過上邊的方法獲取到GridView的插件,然後通過自定義的方法定義一下GridView即可,不會的同學可以先去實現GridView,然後再加上我寫的這些代碼就可以了)
         ViewPager  mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.setCurrentItem(0);// 設置當前pager
 
再加入一個內部類 也是ViewPager的適配器
class MyPagerAdapter extends PagerAdapter {// 這個是ViewPager的自定義適配器。
        @Override
        public int getCount() {// 返回view數量
            return mViewList.size();
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position), 0);
            return mViewList.get(position);
        }
    }
 
 
Layout佈局文件:
主佈局文件中 在對應位置加入這些代碼就可以了。
 <android.support.v4.view.ViewPager
              android:layout_marginLeft="3dip"
                android:layout_marginTop="3dip"
                 android:layout_marginBottom="3dip"
                  android:layout_marginRight="3dip"
                android:id="@+id/viewPager"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >
            </android.support.v4.view.ViewPager>
然後就是兩個GridView自己的佈局文件,這裏只貼出layout1.xml的佈局內容,layout2的就不貼了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    
    android:gravity="center" >
     <GridView 
        android:id="@+id/gridview" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:numColumns="5"
        android:verticalSpacing="5dp" android:horizontalSpacing="5dp"
        android:columnWidth="5dp" android:stretchMode="columnWidth"
        android:gravity="center" 
        >
        </GridView>
</LinearLayout>

 效果圖不能貼因爲這個應用不能隨便放截圖出來,具體效果就想成可以橫向滑動的分頁GridView(廢話),適合我這種 需求固定的 圖標固定的應用,動態展示的話需要再這個基礎上進行修改。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章