• Android:viewPage+Fragment实现模拟微信首页


    一、前言:

    虽然现在很多已经不这么写了,但是这是最底层的东西,我想我还是要好好理解一下的。这篇代码是模拟微信首页底部按钮和ViewPage的联动。记录一下!!

    二、代码理解:

    主页面布局

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. tools:context=".MainActivity">
    9. <androidx.viewpager2.widget.ViewPager2
    10. android:id="@+id/vp"
    11. android:layout_width="match_parent"
    12. android:layout_height="0dp"
    13. android:layout_weight="1"
    14. app:layout_constraintTop_toTopOf="parent" />
    15. <include layout="@layout/bottom_layout" />
    16. LinearLayout>

    对应的java类

    1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    2. public ViewPager2 viewPager;//1.创建viewPage
    3. private LinearLayout llMessage, llContect, llFind, llMine;
    4. private ImageView ivMessage, ivContect, ivFind, ivMine, ivCurrent;
    5. @Override
    6. protected void onCreate(Bundle savedInstanceState) {
    7. super.onCreate(savedInstanceState);
    8. setContentView(R.layout.activity_main);
    9. initPage();//2.viewPage不再是单纯的ui而是混合的,因此要专门定义一个方法来管理ui
    10. initTabView();//初始化tableView控件
    11. }
    12. private void initTabView() {
    13. llMessage = findViewById(R.id.id_tab_weixin);
    14. llMessage.setOnClickListener(this);
    15. llContect = findViewById(R.id.id_tab_contact);
    16. llContect.setOnClickListener(this);
    17. llFind = findViewById(R.id.id_tab_find);
    18. llFind.setOnClickListener(this);
    19. llFind = findViewById(R.id.id_tab_mine);
    20. llFind.setOnClickListener(this);
    21. ivMessage = findViewById(R.id.tab_iv_weixin);
    22. ivContect = findViewById(R.id.tab_iv_contect);
    23. ivFind = findViewById(R.id.tab_iv_find);
    24. ivMine = findViewById(R.id.tab_iv_mine);
    25. ivMessage.setSelected(true);
    26. ivCurrent = ivMessage;//保存当前按钮
    27. }
    28. private void initPage() {
    29. viewPager = findViewById(R.id.vp);//3.找到viewPage
    30. ArrayList fragments = new ArrayList<>();
    31. fragments.add(BlankFragment.newInstance("微信聊天"));
    32. fragments.add(BlankFragment.newInstance("通讯录"));
    33. fragments.add(BlankFragment.newInstance("发现"));
    34. fragments.add(BlankFragment.newInstance("我"));
    35. MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
    36. viewPager.setAdapter(myFragmentPagerAdapter);
    37. //按钮点击触发viewpage页面发生改变
    38. viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    39. @Override
    40. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    41. super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    42. }
    43. @Override
    44. public void onPageSelected(int position) {
    45. super.onPageSelected(position);
    46. changeTab(position);
    47. }
    48. @Override
    49. public void onPageScrollStateChanged(int state) {
    50. super.onPageScrollStateChanged(state);
    51. }
    52. });
    53. }
    54. private void changeTab(int position) {
    55. ivCurrent.setSelected(false);
    56. switch (position) {
    57. case R.id.id_tab_weixin:
    58. case 0:
    59. viewPager.setCurrentItem(0);
    60. ivMessage.setSelected(true);
    61. ivCurrent = ivMessage;
    62. break;
    63. case R.id.id_tab_contact:
    64. case 1:
    65. viewPager.setCurrentItem(1);
    66. ivContect.setSelected(true);
    67. ivCurrent = ivContect;
    68. break;
    69. case R.id.id_tab_find:
    70. case 2:
    71. viewPager.setCurrentItem(2);
    72. ivFind.setSelected(true);
    73. ivCurrent = ivFind;
    74. break;
    75. case R.id.id_tab_mine:
    76. case 3:
    77. viewPager.setCurrentItem(3);
    78. ivMine.setSelected(true);
    79. ivCurrent = ivMine;
    80. break;
    81. }
    82. }
    83. @Override
    84. public void onClick(View view) {
    85. //当点击底部导航按钮的时候改变viewpage的页面,需要把点击的按钮id传入
    86. changeTab(view.getId());
    87. }
    88. }

    底部导航栏设计

    1. "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="55dp"
    5. android:background="@color/white"
    6. android:orientation="horizontal">
    7. <LinearLayout
    8. android:id="@+id/id_tab_weixin"
    9. android:layout_width="0dp"
    10. android:layout_height="match_parent"
    11. android:layout_gravity="center"
    12. android:layout_weight="1"
    13. android:gravity="center"
    14. android:orientation="vertical">
    15. <ImageView
    16. android:id="@+id/tab_iv_weixin"
    17. android:layout_width="32dp"
    18. android:layout_height="32dp"
    19. android:background="@drawable/message_select" />
    20. <TextView
    21. android:id="@+id/text_weixin"
    22. android:layout_width="32dp"
    23. android:layout_height="wrap_content"
    24. android:gravity="center"
    25. android:text="微信" />
    26. LinearLayout>
    27. <LinearLayout
    28. android:id="@+id/id_tab_contact"
    29. android:layout_width="0dp"
    30. android:layout_height="match_parent"
    31. android:layout_gravity="center"
    32. android:layout_weight="1"
    33. android:gravity="center"
    34. android:orientation="vertical">
    35. <ImageView
    36. android:id="@+id/tab_iv_contect"
    37. android:layout_width="32dp"
    38. android:layout_height="32dp"
    39. android:background="@drawable/contect_select" />
    40. <TextView
    41. android:id="@+id/text_contect"
    42. android:layout_width="wrap_content"
    43. android:layout_height="wrap_content"
    44. android:gravity="center"
    45. android:text="通讯录" />
    46. LinearLayout>
    47. <LinearLayout
    48. android:id="@+id/id_tab_find"
    49. android:layout_width="0dp"
    50. android:layout_height="match_parent"
    51. android:layout_gravity="center"
    52. android:layout_weight="1"
    53. android:gravity="center"
    54. android:orientation="vertical">
    55. <ImageView
    56. android:id="@+id/tab_iv_find"
    57. android:layout_width="32dp"
    58. android:layout_height="32dp"
    59. android:background="@drawable/find_select" />
    60. <TextView
    61. android:id="@+id/text_find"
    62. android:layout_width="32dp"
    63. android:layout_height="wrap_content"
    64. android:gravity="center"
    65. android:text="发现" />
    66. LinearLayout>
    67. <LinearLayout
    68. android:id="@+id/id_tab_mine"
    69. android:layout_width="0dp"
    70. android:layout_height="match_parent"
    71. android:layout_gravity="center"
    72. android:layout_weight="1"
    73. android:gravity="center"
    74. android:orientation="vertical">
    75. <ImageView
    76. android:id="@+id/tab_iv_mine"
    77. android:layout_width="32dp"
    78. android:layout_height="32dp"
    79. android:background="@drawable/mine_select" />
    80. <TextView
    81. android:id="@+id/text_mine"
    82. android:layout_width="32dp"
    83. android:layout_height="wrap_content"
    84. android:gravity="center"
    85. android:text="我的" />
    86. LinearLayout>
    87. LinearLayout>

    需要对应的图片资源

    在drawable中新建导航底部按钮选择器

    首页消息

    1. "1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:drawable="@drawable/ic_select_massage" android:state_selected="true"/>
    4. <item android:drawable="@drawable/ic_normal_massages"/>
    5. selector>

    通讯录按钮选择器

    1. "1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:drawable="@drawable/ic_select_contect" android:state_selected="true"/>
    4. <item android:drawable="@drawable/ic_normal_contect"/>
    5. selector>

    发现按钮选择器

    1. "1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:drawable="@drawable/ic_select_find" android:state_selected="true"/>
    4. <item android:drawable="@drawable/ic_normal_find"/>
    5. selector>

    我的按钮选择器

    1. "1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item android:drawable="@drawable/ic_select_mine" android:state_selected="true"/>
    4. <item android:drawable="@drawable/ic_normal_mine"/>
    5. selector>

    三、运行效果:要源码可以私信我

  • 相关阅读:
    知识分享系统
    MybatisPlus
    自定义Lua解析器管理器-------演化脚本V0.5
    openKylin 0.7 尝鲜
    《异常检测——从经典算法到深度学习》29 EasyTSAD: 用于时间序列异常检测模型的工业级基准
    责任链模式
    Docker安装部署Redis集群
    Linux高级IO
    【时时三省】(C语言基础)操作符5
    推荐接口压测报告
  • 原文地址:https://blog.csdn.net/Rssqzqyp/article/details/132811629