Android UI开发篇之 ViewPager+九宫格布局 实现左右滑动

Android UI开发篇之 ViewPager+九宫格布局 实现左右滑动

分类: Android   2542人阅读  评论(4)  收藏  举报
android UI ViewPager 九宫布局

             首先看下UI效果:

              Android UI开发篇之 ViewPager+九宫格布局 实现左右滑动_第1张图片

这个是我在业余开发的新浪微博广场模块的一个实现页面左右滑动的效果,用到的有android.support.v4.view.ViewPager 实现页面切换+ GridView 的九宫格布局。

下面直接贴代码:

1、/WeiBo_ice/res/layout/search_title.xml < 广场标题布局>

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/search_title_layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"   
  6.     android:background="@drawable/titlebar_bg">  
  7.       
  8.     <TextView android:id="@+id/tv_search_name"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center|center_horizontal"  
  12.         android:text="@string/tabbar_square"  
  13.         android:textSize="@dimen/title_text_size"  
  14.         android:textColor="@color/title_text_color"/>  
  15.       
  16.     <FrameLayout android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_gravity="right|center_vertical" >  
  19.           
  20.         <ProgressBar android:id="@+id/titleprogressBar"  
  21.             style="@android:attr/progressBarStyleLarge"  
  22.             android:layout_width="30dip"    
  23.             android:layout_height="30dip"  
  24.             android:visibility="gone"/>  
  25.           
  26.         <ImageView android:id="@+id/title_bt_right"    
  27.             android:layout_width="wrap_content"    
  28.             android:layout_height="wrap_content"     
  29.             android:contentDescription="@string/empty" />  
  30.           
  31.     </FrameLayout>  
  32.   
  33. </FrameLayout>  

2、/WeiBo_ice/res/layout/searchinfo.xml 

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/search_title_layout"   
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.       
  8.     <include android:id="@+id/title"  
  9.         layout="@layout/search_title"  
  10.         />  
  11.       
  12.     <FrameLayout android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent"  
  14.         android:orientation="vertical" >  
  15.           
  16.         <!-- 使用ViewPager -->  
  17.         <android.support.v4.view.ViewPager  
  18.             android:id="@+id/search_viewpager"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_gravity="center" >  
  22.               
  23.         </android.support.v4.view.ViewPager>  
  24.           
  25.         <RelativeLayout android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.             >  
  28.               
  29.             <LinearLayout   
  30.                  android:id="@+id/llguid"  
  31.                  android:layout_width="fill_parent"  
  32.                  android:layout_height="wrap_content"  
  33.                  android:gravity="center"  
  34.                  android:layout_alignParentBottom="true"  
  35.                  android:orientation="horizontal" >  
  36.           
  37.                  <ImageView   
  38.                       android:layout_width="wrap_content"  
  39.                       android:layout_height="wrap_content"  
  40.                       android:src="@drawable/guide_round"  
  41.                       android:clickable="true"  
  42.                       android:layout_margin="8dp"  
  43.                       android:contentDescription="@string/empty"  
  44.                       />  
  45.           
  46.                 <ImageView   
  47.                       android:layout_width="wrap_content"  
  48.                       android:layout_height="wrap_content"  
  49.                       android:src="@drawable/guide_round"  
  50.                       android:clickable="true"  
  51.                       android:layout_margin="8dp"  
  52.                       android:contentDescription="@string/empty"  
  53.                       />  
  54.           
  55.                 <ImageView  
  56.                       android:layout_width="wrap_content"  
  57.                       android:layout_height="wrap_content"  
  58.                       android:src="@drawable/guide_round"  
  59.                       android:clickable="true"  
  60.                       android:layout_margin="8dp"  
  61.                       android:contentDescription="@string/empty"  
  62.                      />  
  63.      
  64.           </LinearLayout>  
  65.             
  66.         </RelativeLayout>  
  67.           
  68.     </FrameLayout>  
  69.               
  70. </LinearLayout>  

3、/WeiBo_ice/res/layout/ninebox.xml  <九宫格布局文件>,共有三个页面切换、所有三个layout 九宫格布局文件< ninebox_2.xml, ninebox_3.xml >、内容相似,这里就只贴出一个。

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <GridView   
  8.         android:id="@+id/myGridView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:numColumns="3"  
  12.         android:verticalSpacing="15dip"  
  13.         android:horizontalSpacing="6dip"  
  14.         android:stretchMode="columnWidth"  
  15.         android:layout_marginTop="20dp"  
  16.         android:gravity="center">  
  17.           
  18.     </GridView>  
  19.   
  20. </LinearLayout>  

4、/WeiBo_ice/res/layout/ninebox_item.xml   九宫格内 每一个格的布局< 包含 图片 和 文字显示>

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <ImageView android:id="@+id/ItemImageView"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:contentDescription="@string/empty"  
  11.         android:layout_gravity="center"/>  
  12.       
  13.     <TextView android:id="@+id/itemTextView"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:textIsSelectable="false"  
  17.         android:layout_gravity="center"  
  18.         android:gravity="center"/>  
  19.   
  20. </LinearLayout>  

5、/WeiBo_ice/src/com/ice/weibo/activity/DiscoveActivity.java  

[java]  view plain copy
  1. package com.ice.weibo.activity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.support.v4.view.PagerAdapter;  
  10. import android.support.v4.view.ViewPager;  
  11. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.widget.GridView;  
  17. import android.widget.ImageView;  
  18. import android.widget.LinearLayout;  
  19. import android.widget.SimpleAdapter;  
  20.   
  21. import com.ice.weibo.R;  
  22. import com.ice.weibo.util.ConstantS;  
  23. /** 
  24.  * 广场UI 
  25.  * @author ice 
  26.  * 
  27.  */  
  28. public class DiscoveActivity extends Activity {  
  29.      
  30.     private ViewPager mViewPager;  
  31.     private LinearLayout ll_1,ll_2,ll_3;  
  32.     private GridView view1,view2,view3;  
  33.       
  34.     private List<View> viewList;  //把需要滑动的页卡添加到这个list中    
  35.     private ImageView[] imageViews;   
  36.     private int currentPosition = 0// 当前位置  
  37.       
  38.     /* 
  39.      * 这里我就偷懒啦、三个切换滑动页面引用的是相同的图标和文字、我就省了去找其他的图片啦  
  40.      * 我们可以定义三组数组分别放每个界面的图标和文字内容、 
  41.      * 然后修改下 下面的 setGridViewAdapter() 方法 就行啦... 
  42.      *  
  43.      */  
  44.     // 定义图标数组  
  45.     private int[] imageRes = {R.drawable.nine_1,R.drawable.nine_2,R.drawable.nine_3,  
  46.             R.drawable.nine_4,R.drawable.nine_5,R.drawable.nine_6,  
  47.             R.drawable.nine_7,R.drawable.nine_8,R.drawable.nine_9};  
  48.     //定义标题数组  
  49.     private String[] itemName = {"微信","微音乐","微魅儿",  
  50.             "微通讯","微短信","微烦皂",  
  51.             "微盟主","微爱心","微互粉"  
  52.             };  
  53.       
  54.     @Override  
  55.     protected void onCreate(Bundle savedInstanceState) {  
  56.           
  57.         super.onCreate(savedInstanceState);  
  58.         setContentView(R.layout.searchinfo);  
  59.         initViews();  
  60.         Log.d(ConstantS.TAG, "currentPosition:"+currentPosition++);  
  61.     }  
  62.   
  63.     public void initViews() {  
  64.         mViewPager = (ViewPager) findViewById(R.id.search_viewpager);  
  65.         LayoutInflater lf = getLayoutInflater().from(this);  
  66.         ll_1 = (LinearLayout) lf.inflate(R.layout.ninebox, null);  
  67.         view1 = (GridView) ll_1.findViewById(R.id.myGridView);  
  68.           
  69.         ll_2 = (LinearLayout) lf.inflate(R.layout.ninebox_2, null);  
  70.         view2 = (GridView) ll_2.findViewById(R.id.myGridView2);  
  71.           
  72.         ll_3 = (LinearLayout) lf.inflate(R.layout.ninebox_3, null);  
  73.         view3 = (GridView) ll_3.findViewById(R.id.myGridView3);  
  74.           
  75.         viewList = new ArrayList<View>();  // 将要分页显示的View装入数组中    
  76.         viewList.add(ll_1);  
  77.         viewList.add(ll_2);  
  78.         viewList.add(ll_3);  
  79.           
  80.         setGridViewAdapter(view1);  
  81.         setGridViewAdapter(view2);  
  82.         setGridViewAdapter(view3);  
  83.           
  84.         mViewPager.setAdapter(new MyPagerAdapter(viewList));  
  85.         mViewPager.setOnPageChangeListener(new myOnPageChangeListener());  
  86.           
  87.         LinearLayout llguid = (LinearLayout) findViewById(R.id.llguid);  
  88.         imageViews = new ImageView[viewList.size()];  
  89.         for(int i= 0;i<viewList.size();i++){  
  90.             imageViews[i] = (ImageView) llguid.getChildAt(i);  
  91.             imageViews[i].setEnabled(true);  
  92.             imageViews[i].setTag(i);  
  93.         }  
  94.         imageViews[currentPosition].setEnabled(false);  
  95.     }  
  96.   
  97.     /** 
  98.      *  设置GridView适配器 
  99.      */  
  100.     private void setGridViewAdapter(GridView mGridView) {  
  101.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  102.         int length = itemName.length;  
  103.         for(int i=0;i<length;i++){  
  104.             HashMap<String,Object> map = new HashMap<String, Object>();  
  105.             map.put("ItemImageView", imageRes[i]);  
  106.             map.put("ItemTextView", itemName[i]);  
  107.             data.add(map);  
  108.         }  
  109.           
  110.         SimpleAdapter simpleAdapter = new SimpleAdapter(this,   
  111.                 data,   
  112.                 R.layout.ninebox_item,   
  113.                 new String[]{"ItemImageView","ItemTextView"},   
  114.                 new int[]{R.id.ItemImageView,R.id.itemTextView});  
  115.         mGridView.setAdapter(simpleAdapter);  
  116.     }  
  117.       
  118.       
  119.     class MyPagerAdapter extends PagerAdapter{  
  120.         List<View> viewList;  
  121.           
  122.         public MyPagerAdapter(List<View> viewList) {  
  123.             this.viewList = viewList;  
  124.         }  
  125.   
  126.         @Override  
  127.         public int getCount() {  
  128.             // TODO Auto-generated method stub  
  129.             return viewList.size();  
  130.         }  
  131.   
  132.         @Override  
  133.         public boolean isViewFromObject(View arg0, Object arg1) {  
  134.             // TODO Auto-generated method stub  
  135.             return arg0 == arg1;  
  136.         }  
  137.   
  138.         @Override  
  139.         public void destroyItem(View container, int position, Object object) {  
  140.             // TODO Auto-generated method stub  
  141.             ((ViewPager)container).removeView(viewList.get(position));  
  142.         }  
  143.   
  144.         @Override  
  145.         public Object instantiateItem(ViewGroup container, int position) {  
  146.             // TODO Auto-generated method stub  
  147.             container.addView(viewList.get(position));  
  148.             // Log.d(ConstantS.TAG, "ViewPager--Position:"+position);  
  149.             return viewList.get(position);  
  150.         }  
  151.     }  
  152.       
  153.     class myOnPageChangeListener implements OnPageChangeListener{  
  154.   
  155.         /** 
  156.          *  当滑动状态改变时调用    
  157.          */  
  158.         @Override  
  159.         public void onPageScrollStateChanged(int arg0) {  
  160.             // TODO Auto-generated method stub  
  161.               
  162.         }  
  163.   
  164.         /** 
  165.          * 当当前页面被滑动时调用  
  166.          */  
  167.         @Override  
  168.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  169.             // TODO Auto-generated method stub  
  170.               
  171.         }  
  172.   
  173.         /** 
  174.          * 当新的页面被选中时调用  
  175.          */  
  176.         @Override  
  177.         public void onPageSelected(int arg0) {  
  178.             Log.d(ConstantS.TAG, "ViewPager--当前是第几页:"+arg0);  
  179.             for(int i=0;i<imageViews.length;i++){  
  180.                 if(i == arg0){  
  181.                     imageViews[arg0].setEnabled(true);  
  182.                 }else{  
  183.                     imageViews[i].setEnabled(false);  
  184.                 }  
  185.             }  
  186.         }  
  187.     }  
  188.       
  189. }  

6、/WeiBo_ice/res/drawable-mdpi/guide_round.xml  这个是控制界面切换时下面三个向导的小圆图标的显示效果。< 在searchinfo.xml 中有被引用>

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_enabled="true" android:drawable="@drawable/guide_round_current"></item>  
  4.     <item android:state_enabled="false" android:drawable="@drawable/guide_round_default"></item>  
  5.   
  6. </selector>  

你可能感兴趣的:(android,android,viewpager)