Android 高仿QQ 界面滑动效果


点击或者滑动切换画面,用ViewPager实现,

首先是布局文件:

  1. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.     android:orientation="vertical" > 
  5.     <LinearLayout 
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="62dip" 
  8.         android:orientation="vertical"   
  9.         android:background="@drawable/top_theme_blue"> 
  10.         <LinearLayout 
  11.             android:layout_width="match_parent" 
  12.             android:layout_height="36dip" 
  13.             android:orientation="horizontal"   
  14.             android:gravity="center_vertical"> 
  15.             <ImageView 
  16.                 android:id="@+id/main_avatar" 
  17.                 android:layout_width="32dip" 
  18.                 android:layout_height="32dip" 
  19.                 android:src="@drawable/avatar" /> 
  20.             <TextView   
  21.                 android:id="@+id/main_nick" 
  22.                 android:layout_width="wrap_content" 
  23.                 android:layout_height="wrap_content" 
  24.                 android:textColor="#FFFFFF" 
  25.                 android:text="Vestigge"/> 
  26.             <ImageView   
  27.                 android:layout_width="14dip" 
  28.                 android:layout_height="14dip" 
  29.                 android:src="@drawable/status_online"/> 
  30.         </LinearLayout> 
  31.         <LinearLayout 
  32.             android:layout_width="match_parent" 
  33.             android:layout_height="26dip" 
  34.             android:orientation="horizontal"   
  35.             android:gravity="bottom"> 
  36.             <RadioGroup     
  37.                 android:id="@+id/main_radiogroup" 
  38.                 android:orientation="horizontal" 
  39.                 android:paddingTop="1dp"   
  40.                 android:layout_width="fill_parent"     
  41.                 android:layout_height="wrap_content">   
  42.                 <RadioButton     
  43.                     android:id="@+id/main_radio_recent"     
  44.                     android:checked="true"     
  45.                     android:text="动态"   
  46.                     android:textColor="@color/tab_text" 
  47.                     android:drawableBottom="@drawable/top_tab_selector" 
  48.                     style="@style/radio_style"/>       
  49.                 <RadioButton     
  50.                     android:id="@+id/main_radio_buddy"   
  51.                     android:text="群组" 
  52.                     android:textColor="@color/tab_text"   
  53.                     android:drawableBottom="@drawable/top_tab_selector" 
  54.                     style="@style/radio_style"/>   
  55.                 <RadioButton     
  56.                     android:id="@+id/main_radio_group"     
  57.                     android:text="好友"   
  58.                     android:textColor="@color/tab_text" 
  59.                     android:drawableBottom="@drawable/top_tab_selector" 
  60.                     style="@style/radio_style" 
  61.                     android:checked="true"/> 
  62.                 <RadioButton     
  63.                     android:id="@+id/main_radio_trends"     
  64.                     android:text="会话"   
  65.                     android:textColor="@color/tab_text" 
  66.                     android:drawableBottom="@drawable/top_tab_selector" 
  67.                     style="@style/radio_style"/> 
  68.             </RadioGroup>     
  69.         </LinearLayout> 
  70.     </LinearLayout> 
  71.     <android.support.v4.view.ViewPager 
  72.         android:id="@+id/main_viewpager" 
  73.         android:layout_width="match_parent" 
  74.         android:layout_height="match_parent" > 
  75.     </android.support.v4.view.ViewPager>     
  76. </LinearLayout> 

代码:

  1. public class MainActivity extends ActivityGroup { 
  2.     private static final String TRENDS="动态"
  3.     private static final String GROUP="群组"
  4.     private static final String BUDDY="好友"
  5.     private static final String RECENT="会话"
  6.     private ArrayList<View> pageViews; 
  7.     private RadioGroup radioGroup; 
  8.     private ViewPager viewPager; 
  9.  
  10.     public void onCreate(Bundle savedInstanceState) { 
  11.         super.onCreate(savedInstanceState); 
  12.         requestWindowFeature(Window.FEATURE_NO_TITLE); 
  13.         setContentView(R.layout.activity_main); 
  14.          
  15.         initView(); 
  16.         viewPager=(ViewPager) findViewById(R.id.main_viewpager); 
  17.         viewPager.setAdapter(new PagerAdapter(){ 
  18.             public int getCount() { 
  19.                 return pageViews.size(); 
  20.             } 
  21.             public boolean isViewFromObject(View view, Object objcet) { 
  22.                 return view==objcet; 
  23.             } 
  24.             //这里会对需要进行水平切换的页面进行了加载和初始化 android:tileMode="repeat"  
  25.             public Object instantiateItem(View view, int id) { 
  26.                 ((ViewPager)view).addView(pageViews.get(id)); 
  27.                 return pageViews.get(id); 
  28.             } 
  29.             public void destroyItem(View view, int id, Object arg2) {   
  30.                 ((ViewPager) view).removeView(pageViews.get(id));   
  31.             } 
  32.         }); 
  33.         viewPager.setCurrentItem(2);//默认显示的是好友页面  
  34.         radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup); 
  35.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
  36.             public void onCheckedChanged(RadioGroup group, int checkedId) { 
  37.                 setClick(checkedId); 
  38.             } 
  39.         }); 
  40.     } 
  41.      
  42.     void initView() { 
  43.         pageViews=new ArrayList<View>(); 
  44.         View view1 = getLocalActivityManager().startActivity(TRENDS, 
  45.                 new Intent(this, TrendsActivity.class)).getDecorView(); 
  46.         View view2 = getLocalActivityManager().startActivity(GROUP, 
  47.                 new Intent(this, GroupActivity.class)).getDecorView(); 
  48.         View view3 = getLocalActivityManager().startActivity(BUDDY, 
  49.                 new Intent(this, BuddyActivity.class)).getDecorView(); 
  50.         View view4 = getLocalActivityManager().startActivity(RECENT, 
  51.                 new Intent(this, RecentActivity.class)).getDecorView(); 
  52.         pageViews.add(0,view1); 
  53.         pageViews.add(1,view2); 
  54.         pageViews.add(2,view3); 
  55.         pageViews.add(3,view4); 
  56.     } 
  57.  
  58.     public void setClick(int id) { 
  59.         switch(id){ 
  60.         case R.id.main_radio_trends: 
  61.             viewPager.setCurrentItem(0); 
  62.             break
  63.         case R.id.main_radio_group: 
  64.             viewPager.setCurrentItem(1); 
  65.             break
  66.         case R.id.main_radio_buddy: 
  67.             viewPager.setCurrentItem(2); 
  68.             break
  69.         case R.id.main_radio_recent: 
  70.             viewPager.setCurrentItem(3); 
  71.             break
  72.         } 
  73.     } 
  74.     @Override 
  75.     public boolean onCreateOptionsMenu(Menu menu) { 
  76.         getMenuInflater().inflate(R.menu.activity_main, menu); 
  77.         return true
  78.     } 

相关内容