歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 高仿QQ 界面滑動效果

Android 高仿QQ 界面滑動效果

日期:2017/3/1 10:07:50   编辑:Linux編程

點擊或者滑動切換畫面,用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. />
  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. />
  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. android:checked="true"/>
  61. <RadioButton
  62. android:id="@+id/main_radio_trends"
  63. android:text="會話"
  64. android:textColor="@color/tab_text"
  65. android:drawableBottom="@drawable/top_tab_selector"
  66. />
  67. </RadioGroup>
  68. </LinearLayout>
  69. </LinearLayout>
  70. <android.support.v4.view.ViewPager
  71. android:id="@+id/main_viewpager"
  72. android:layout_width="match_parent"
  73. android:layout_height="match_parent" >
  74. </android.support.v4.view.ViewPager>
  75. </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. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. requestWindowFeature(Window.FEATURE_NO_TITLE);
  12. setContentView(R.layout.activity_main);
  13. initView();
  14. viewPager=(ViewPager) findViewById(R.id.main_viewpager);
  15. viewPager.setAdapter(new PagerAdapter(){
  16. public int getCount() {
  17. return pageViews.size();
  18. }
  19. public boolean isViewFromObject(View view, Object objcet) {
  20. return view==objcet;
  21. }
  22. //這裡會對需要進行水平切換的頁面進行了加載和��始化 android:tileMode="repeat"
  23. public Object instantiateItem(View view, int id) {
  24. ((ViewPager)view).addView(pageViews.get(id));
  25. return pageViews.get(id);
  26. }
  27. public void destroyItem(View view, int id, Object arg2) {
  28. ((ViewPager) view).removeView(pageViews.get(id));
  29. }
  30. });
  31. viewPager.setCurrentItem(2);//默認顯示的是好友頁面
  32. radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
  33. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  34. public void onCheckedChanged(RadioGroup group, int checkedId) {
  35. setClick(checkedId);
  36. }
  37. });
  38. }
  39. void initView() {
  40. pageViews=new ArrayList<View>();
  41. View view1 = getLocalActivityManager().startActivity(TRENDS,
  42. new Intent(this, TrendsActivity.class)).getDecorView();
  43. View view2 = getLocalActivityManager().startActivity(GROUP,
  44. new Intent(this, GroupActivity.class)).getDecorView();
  45. View view3 = getLocalActivityManager().startActivity(BUDDY,
  46. new Intent(this, BuddyActivity.class)).getDecorView();
  47. View view4 = getLocalActivityManager().startActivity(RECENT,
  48. new Intent(this, RecentActivity.class)).getDecorView();
  49. pageViews.add(0,view1);
  50. pageViews.add(1,view2);
  51. pageViews.add(2,view3);
  52. pageViews.add(3,view4);
  53. }
  54. public void setClick(int id) {
  55. switch(id){
  56. case R.id.main_radio_trends:
  57. viewPager.setCurrentItem(0);
  58. break;
  59. case R.id.main_radio_group:
  60. viewPager.setCurrentItem(1);
  61. break;
  62. case R.id.main_radio_buddy:
  63. viewPager.setCurrentItem(2);
  64. break;
  65. case R.id.main_radio_recent:
  66. viewPager.setCurrentItem(3);
  67. break;
  68. }
  69. }
  70. @Override
  71. public boolean onCreateOptionsMenu(Menu menu) {
  72. getMenuInflater().inflate(R.menu.activity_main, menu);
  73. return true;
  74. }
  75. }
Copyright © Linux教程網 All Rights Reserved