Android~仿微信(Fragment+ViewPager)

布满荆棘的人生 2022-10-05 11:46 347阅读 0赞

仿微信(Fragment+ViewPager),实现效果,滑动或者点击,页面随Tab顶部切换,效果如下所示:
在这里插入图片描述
思维导图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

详细代码如下:
1、MainActivity .java

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private TextView title;
  3. private TextView mTextHome;
  4. private TextView mTextCommuni;
  5. private TextView mTextFind;
  6. private TextView mTextProfile;
  7. private ImageView mImgHome;
  8. private ImageView mImgCommuni;
  9. private ImageView mImgFind;
  10. private ImageView mImgProfile;
  11. private LinearLayout mLinearHome;
  12. private LinearLayout mLinearCommubi;
  13. private LinearLayout mLinearFind;
  14. private LinearLayout mLinearProfile;
  15. private ViewPager viewPager;
  16. private HomeFragment homeFragment;
  17. private CommuniFragment communiFragment;
  18. private FindFragment findFragment;
  19. private ProfileFragment profileFragment;
  20. private FragmentManager manager;
  21. private ViewPagerAdapter adapter;
  22. private List<Fragment> mList;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. initView();
  28. setSelect(0);
  29. }
  30. // 初始化界面
  31. private void initView() {
  32. // 实例化对象
  33. title = findViewById(R.id.titleTop);
  34. mImgHome = findViewById(R.id.imgHome);
  35. mImgCommuni = findViewById(R.id.imgCommuni);
  36. mImgFind = findViewById(R.id.imgFind);
  37. mImgProfile = findViewById(R.id.imgProfile);
  38. mTextHome = findViewById(R.id.textHome);
  39. mTextCommuni = findViewById(R.id.textCommuni);
  40. mTextFind = findViewById(R.id.textFind);
  41. mTextProfile = findViewById(R.id.textProfile);
  42. mLinearHome = findViewById(R.id.linearHome);
  43. mLinearCommubi = findViewById(R.id.linearCommuni);
  44. mLinearFind = findViewById(R.id.linearFind);
  45. mLinearProfile = findViewById(R.id.linearProfile);
  46. viewPager = findViewById(R.id.viewPager);
  47. mList = new ArrayList<Fragment>();
  48. manager = getSupportFragmentManager();
  49. // 注册监听事件
  50. mLinearHome.setOnClickListener(this);
  51. mLinearCommubi.setOnClickListener(this);
  52. mLinearFind.setOnClickListener(this);
  53. mLinearProfile.setOnClickListener(this);
  54. // 设置数据源
  55. homeFragment = new HomeFragment();
  56. communiFragment = new CommuniFragment();
  57. findFragment = new FindFragment();
  58. profileFragment = new ProfileFragment();
  59. mList.add(homeFragment);
  60. mList.add(communiFragment);
  61. mList.add(findFragment);
  62. mList.add(profileFragment);
  63. // 设置适配器
  64. adapter = new ViewPagerAdapter(manager, mList);
  65. viewPager.setAdapter(adapter);
  66. // 设置滑动监听
  67. viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  68. @Override
  69. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  70. }
  71. @Override
  72. public void onPageSelected(int position) {
  73. switch (position) {
  74. case 0:
  75. setSelect(0);
  76. break;
  77. case 1:
  78. setSelect(1);
  79. break;
  80. case 2:
  81. setSelect(2);
  82. break;
  83. case 3:
  84. setSelect(3);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. @Override
  91. public void onPageScrollStateChanged(int state) {
  92. }
  93. });
  94. }
  95. @Override
  96. public void onClick(View v) {
  97. switch (v.getId()) {
  98. case R.id.linearHome:
  99. setSelect(0);
  100. break;
  101. case R.id.linearCommuni:
  102. setSelect(1);
  103. break;
  104. case R.id.linearFind:
  105. setSelect(2);
  106. break;
  107. case R.id.linearProfile:
  108. setSelect(3);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. private void setSelect(int position) {
  115. resetAll();
  116. switch (position) {
  117. case 0:
  118. mImgHome.setImageResource(R.drawable.ic_home_active);
  119. mTextHome.setTextColor(getResources().getColor(R.color.green));
  120. title.setText(mTextHome.getText().toString());
  121. break;
  122. case 1:
  123. mImgCommuni.setImageResource(R.drawable.ic_communi_activite);
  124. mTextCommuni.setTextColor(getResources().getColor(R.color.green));
  125. title.setText(mTextCommuni.getText().toString());
  126. break;
  127. case 2:
  128. mImgFind.setImageResource(R.drawable.ic_find_active);
  129. mTextFind.setTextColor(getResources().getColor(R.color.green));
  130. title.setText(mTextFind.getText().toString());
  131. break;
  132. case 3:
  133. mImgProfile.setImageResource(R.drawable.ic_profile_activite);
  134. mTextProfile.setTextColor(getResources().getColor(R.color.green));
  135. title.setText(mTextProfile.getText().toString());
  136. break;
  137. default:
  138. break;
  139. }
  140. viewPager.setCurrentItem(position);
  141. }
  142. private void resetAll() {
  143. mImgHome.setImageResource(R.drawable.ic_home);
  144. mTextHome.setTextColor(getResources().getColor(R.color.gray));
  145. mImgCommuni.setImageResource(R.drawable.ic_communi);
  146. mTextCommuni.setTextColor(getResources().getColor(R.color.gray));
  147. mImgFind.setImageResource(R.drawable.ic_find);
  148. mTextFind.setTextColor(getResources().getColor(R.color.gray));
  149. mImgProfile.setImageResource(R.drawable.ic_profile);
  150. mTextProfile.setTextColor(getResources().getColor(R.color.gray));
  151. }
  152. }

2、activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
  2. <TextView android:id="@+id/titleTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="测试" android:textSize="21sp" />
  3. <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
  4. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
  5. <LinearLayout android:id="@+id/linearHome" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical">
  6. <ImageView android:id="@+id/imgHome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_home" />
  7. <TextView android:id="@+id/textHome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" />
  8. </LinearLayout>
  9. <LinearLayout android:id="@+id/linearCommuni" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical">
  10. <ImageView android:id="@+id/imgCommuni" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_communi" />
  11. <TextView android:id="@+id/textCommuni" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通讯录" />
  12. </LinearLayout>
  13. <LinearLayout android:id="@+id/linearFind" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical">
  14. <ImageView android:id="@+id/imgFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_find" />
  15. <TextView android:id="@+id/textFind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发现" />
  16. </LinearLayout>
  17. <LinearLayout android:id="@+id/linearProfile" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical">
  18. <ImageView android:id="@+id/imgProfile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_profile" />
  19. <TextView android:id="@+id/textProfile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的" />
  20. </LinearLayout>
  21. </LinearLayout>
  22. </LinearLayout>

3、ViewPagerAdapter (适配器)

  1. public class ViewPagerAdapter extends FragmentStatePagerAdapter {
  2. private List<Fragment> mList;
  3. public ViewPagerAdapter(FragmentManager fm, List<Fragment> list) {
  4. super(fm);
  5. mList = list;
  6. }
  7. @Override
  8. public Fragment getItem(int position) {
  9. return mList.get(position);
  10. }
  11. @Override
  12. public int getCount() {
  13. return mList.size();
  14. }
  15. }

4、Fragment 类(其中一个)

  1. public class HomeFragment extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
  5. View view = inflater.inflate(R.layout.fragment_home, container, false);
  6. return view;
  7. }
  8. }

5、fragment_home.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  2. <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="我是首页" />
  3. </LinearLayout>

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读