Android 使用Gallery实现Tab

来源:互联网 发布:知乎福利大合集 编辑:程序博客网 时间:2024/05/24 15:39
main.xml布局文件: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:layout_marginTop="30dp"  
  7.         android:unselectedAlpha="1" android:spacing="1dip" />  
  8. </LinearLayout>  


values/attrs.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6. </resources>  


values/strings.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     <string name="hello">Hello World, Date!</string>    
  4.     <string name="app_name">丸子-Widget</string>    
  5. </resources>    


drawable/tab_button_select.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#FF1B1B1B" android:endColor="#FF969696"  
  4.         android:angle="90.0">  
  5.     </gradient>  
  6. </shape>  


drawable/tab_button_unselect.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <gradient android:startColor="#FF000000" android:endColor="#FF474747"  
  4.         android:angle="90.0">  
  5.     </gradient>  
  6. </shape>  


IaiaiActivity.java类: 
Java代码  收藏代码
  1. package com.iaiai.activity;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.content.res.TypedArray;  
  10. import android.graphics.Color;  
  11. import android.os.Bundle;  
  12. import android.view.Gravity;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AdapterView;  
  16. import android.widget.AdapterView.OnItemClickListener;  
  17. import android.widget.BaseAdapter;  
  18. import android.widget.Gallery;  
  19. import android.widget.TextView;  
  20.   
  21. /** 
  22.  *  
  23.  * <p> 
  24.  * Title: IaiaiActivity.java 
  25.  * </p> 
  26.  * <p> 
  27.  * E-Mail: 176291935@qq.com 
  28.  * </p> 
  29.  * <p> 
  30.  * QQ: 176291935 
  31.  * </p> 
  32.  * <p> 
  33.  * Http: iaiai.iteye.com 
  34.  * </p> 
  35.  * <p> 
  36.  * Create time: 2011-6-26 
  37.  * </p> 
  38.  *  
  39.  * @author 丸子 
  40.  * @version 0.0.1 
  41.  */  
  42. public class IaiaiActivity extends Activity {  
  43.   
  44.     private Gallery gallery;  
  45.     private TabAdapter textAdapter;  
  46.   
  47.     private static final String[] PROGRAM_NAMES = { "中央一台""中央二台""中央三台",  
  48.             "中央四台""中央五台""中央六台""中央七台""中央八台", };  
  49.   
  50.     @Override  
  51.     public void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.main);  
  54.   
  55.         gallery = (Gallery) findViewById(R.id.gallery);  
  56.         textAdapter = new TabAdapter(this, Arrays.asList(PROGRAM_NAMES));  
  57.         gallery.setAdapter(textAdapter);  
  58.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  59.   
  60.             @Override  
  61.             public void onItemClick(AdapterView<?> parent, View view,  
  62.                     int position, long id) {  
  63.                 TabAdapter adapter = (TabAdapter) parent.getAdapter();  
  64.                 adapter.setSelectedPos(position);  
  65.             }  
  66.   
  67.         });  
  68.     }  
  69.   
  70.     public class TabAdapter extends BaseAdapter {  
  71.         private Context mContext;  
  72.         private List<String> mList;  
  73.         private int mSelectedPos;  
  74.   
  75.         public TabAdapter(Context context, List<String> list) {  
  76.             mContext = context;  
  77.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery);  
  78.             a.recycle();  
  79.             if (list == null)  
  80.                 list = Collections.emptyList();  
  81.             mList = list;  
  82.         }  
  83.   
  84.         public void setSelectedPos(int pos) {  
  85.             if (pos != mSelectedPos) {  
  86.                 mSelectedPos = pos;  
  87.                 notifyDataSetChanged();  
  88.             }  
  89.         }  
  90.   
  91.         public int getSelectedPos() {  
  92.             return mSelectedPos;  
  93.         }  
  94.   
  95.         public int getCount() {  
  96.             return mList.size();  
  97.         }  
  98.   
  99.         public Object getItem(int position) {  
  100.             return mList.get(position);  
  101.         }  
  102.   
  103.         public long getItemId(int position) {  
  104.             return position;  
  105.         }  
  106.   
  107.         public View getView(int position, View convertView, ViewGroup parent) {  
  108.             TextView text = null;  
  109.             if (convertView == null) {  
  110.                 text = new TextView(mContext);  
  111.             } else {  
  112.                 text = (TextView) convertView;  
  113.             }  
  114.   
  115.             text.setTextColor(Color.WHITE);  
  116.             text.setText(mList.get(position));  
  117.   
  118.             text.setLayoutParams(new Gallery.LayoutParams(10240));  
  119.             text.setGravity(Gravity.CENTER);  
  120.   
  121.             if (position == mSelectedPos)  
  122.                 text.setBackgroundResource(R.drawable.tab_button_select);  
  123.             else  
  124.                 text.setBackgroundResource(R.drawable.tab_button_unselect);  
  125.   
  126.             return text;  
  127.         }  
  128.     }  
  129.   
  130. }  


运行结果: 

原创粉丝点击