From 558cf6d98d5649ba3b999d4612b65b2fabb056b9 Mon Sep 17 00:00:00 2001 From: lihongwei Date: Wed, 18 Sep 2024 15:10:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5ui?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 7 +- .../player/adapter/HomeViewPagerAdapter.java | 36 ++++ .../com/hi/music/player/ui/HomeActivity.java | 93 ++++++++ .../com/hi/music/player/ui/HomeFragment.java | 22 ++ .../hi/music/player/ui/ProfileFragment.java | 27 +++ .../hi/music/player/ui/SearchFragment.java | 27 +++ app/src/main/res/drawable/home_background.xml | 15 ++ app/src/main/res/drawable/home_select.xml | 19 ++ app/src/main/res/drawable/home_unselect.xml | 11 + .../main/res/drawable/ic_menu_hamburger.xml | 12 ++ app/src/main/res/drawable/profile_select.xml | 14 ++ .../main/res/drawable/profile_unselect.xml | 10 + app/src/main/res/drawable/right_arrow.xml | 11 + app/src/main/res/drawable/round_rectangle.xml | 11 + .../drawable/rounded_rectangle_tab_layout.xml | 36 ++++ app/src/main/res/drawable/search_select.xml | 22 ++ app/src/main/res/drawable/search_unselect.xml | 15 ++ app/src/main/res/layout/activity_home.xml | 24 +++ app/src/main/res/layout/fragment_home.xml | 204 ++++++++++++++++++ app/src/main/res/layout/fragment_profile.xml | 179 +++++++++++++++ app/src/main/res/layout/fragment_search.xml | 14 ++ app/src/main/res/layout/home_tab_custom.xml | 11 + app/src/main/res/values/strings.xml | 9 + 24 files changed, 828 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/hi/music/player/adapter/HomeViewPagerAdapter.java create mode 100644 app/src/main/java/com/hi/music/player/ui/HomeActivity.java create mode 100644 app/src/main/java/com/hi/music/player/ui/HomeFragment.java create mode 100644 app/src/main/java/com/hi/music/player/ui/ProfileFragment.java create mode 100644 app/src/main/java/com/hi/music/player/ui/SearchFragment.java create mode 100644 app/src/main/res/drawable/home_background.xml create mode 100644 app/src/main/res/drawable/home_select.xml create mode 100644 app/src/main/res/drawable/home_unselect.xml create mode 100644 app/src/main/res/drawable/ic_menu_hamburger.xml create mode 100644 app/src/main/res/drawable/profile_select.xml create mode 100644 app/src/main/res/drawable/profile_unselect.xml create mode 100644 app/src/main/res/drawable/right_arrow.xml create mode 100644 app/src/main/res/drawable/round_rectangle.xml create mode 100644 app/src/main/res/drawable/rounded_rectangle_tab_layout.xml create mode 100644 app/src/main/res/drawable/search_select.xml create mode 100644 app/src/main/res/drawable/search_unselect.xml create mode 100644 app/src/main/res/layout/activity_home.xml create mode 100644 app/src/main/res/layout/fragment_home.xml create mode 100644 app/src/main/res/layout/fragment_profile.xml create mode 100644 app/src/main/res/layout/fragment_search.xml create mode 100644 app/src/main/res/layout/home_tab_custom.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6d78c91..831d7c7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -40,6 +40,7 @@ dependencies { implementation("androidx.appcompat:appcompat:1.7.0") implementation("com.google.android.material:material:1.12.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") + implementation("androidx.activity:activity:1.9.1") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.2.1") androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 765b35a..6240991 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,8 @@ - + + - + diff --git a/app/src/main/java/com/hi/music/player/adapter/HomeViewPagerAdapter.java b/app/src/main/java/com/hi/music/player/adapter/HomeViewPagerAdapter.java new file mode 100644 index 0000000..4690ef1 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/adapter/HomeViewPagerAdapter.java @@ -0,0 +1,36 @@ +package com.hi.music.player.adapter; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.viewpager2.adapter.FragmentStateAdapter; + +import com.hi.music.player.ui.HomeFragment; +import com.hi.music.player.ui.ProfileFragment; +import com.hi.music.player.ui.SearchFragment; + +import java.util.ArrayList; +import java.util.List; + +public class HomeViewPagerAdapter extends FragmentStateAdapter { + + private final List fragments = new ArrayList<>(); + + public HomeViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) { + super(fragmentActivity); + fragments.add(new HomeFragment()); + fragments.add(new SearchFragment()); + fragments.add(new ProfileFragment()); + } + + @NonNull + @Override + public Fragment createFragment(int position) { + return fragments.get(position); + } + + @Override + public int getItemCount() { + return fragments.size(); + } +} diff --git a/app/src/main/java/com/hi/music/player/ui/HomeActivity.java b/app/src/main/java/com/hi/music/player/ui/HomeActivity.java new file mode 100644 index 0000000..48b76b3 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/ui/HomeActivity.java @@ -0,0 +1,93 @@ +package com.hi.music.player.ui; + +import android.os.Bundle; +import android.view.LayoutInflater; + +import androidx.appcompat.app.AppCompatActivity; + +import com.google.android.material.tabs.TabLayout; +import com.google.android.material.tabs.TabLayoutMediator; +import com.hi.music.player.R; +import com.hi.music.player.adapter.HomeViewPagerAdapter; +import com.hi.music.player.databinding.ActivityHomeBinding; +import com.hi.music.player.databinding.HomeTabCustomBinding; + +public class HomeActivity extends BaseActivity { + + // 图标数组定义为类成员,避免重复 + private final int[] defaultIcons = { + R.drawable.home_unselect, + R.drawable.search_unselect, + R.drawable.profile_unselect + }; + + private final int[] selectedIcons = { + R.drawable.home_select, + R.drawable.search_select, + R.drawable.profile_select + }; + + @Override + protected ActivityHomeBinding getViewBinding() { + return ActivityHomeBinding.inflate(getLayoutInflater()); + } + + @Override + protected void onCreateInit() { + initView(); // 可以在这里初始化视图 + } + + @Override + public boolean isFullScreen() { + return false; + } + + @Override + public boolean statusBarLight() { + return false; + } + + public void initView() { + HomeViewPagerAdapter adapter = new HomeViewPagerAdapter(this); + vb.homeViewPager.setAdapter(adapter); + + // 设置TabLayout的图标 + new TabLayoutMediator(vb.homeTabLayout, vb.homeViewPager, (tab, position) -> { + HomeTabCustomBinding tabBinding = HomeTabCustomBinding.inflate(LayoutInflater.from(this)); + tab.setCustomView(tabBinding.getRoot()); + tabBinding.homeIcon.setImageResource(defaultIcons[position]); // 默认图标 + }).attach(); + + // 添加Tab选中与未选中事件监听器 + vb.homeTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { + @Override + public void onTabSelected(TabLayout.Tab tab) { + updateTabIcon(tab, true); // 更新选中的图标 + } + + @Override + public void onTabUnselected(TabLayout.Tab tab) { + updateTabIcon(tab, false); // 恢复未选中图标 + } + + @Override + public void onTabReselected(TabLayout.Tab tab) { + // 可选:重复选择Tab时的操作 + } + }); + + // 设置默认选中第一个 + TabLayout.Tab firstTab = vb.homeTabLayout.getTabAt(0); + if (firstTab != null) { + firstTab.select(); + updateTabIcon(firstTab, true); // 设置选中的图标 + } + } + + private void updateTabIcon(TabLayout.Tab tab, boolean isSelected) { + HomeTabCustomBinding tabBinding = HomeTabCustomBinding.bind(tab.getCustomView()); + int position = tab.getPosition(); + tabBinding.homeIcon.setImageResource(isSelected ? selectedIcons[position] : defaultIcons[position]); + } +} + diff --git a/app/src/main/java/com/hi/music/player/ui/HomeFragment.java b/app/src/main/java/com/hi/music/player/ui/HomeFragment.java new file mode 100644 index 0000000..440efd7 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/ui/HomeFragment.java @@ -0,0 +1,22 @@ +package com.hi.music.player.ui; + +import com.hi.music.player.R; +import com.hi.music.player.databinding.FragmentHomeBinding; + +public class HomeFragment extends BaseFragment { + + @Override + protected FragmentHomeBinding getFragmentVb() { + // 返回正确的 ViewBinding 实例 + return FragmentHomeBinding.inflate(getLayoutInflater()); + } + + @Override + protected void initView() { + // 在这里进行视图的初始化操作 + + //标题导航栏颜色设置 + Vb.toolbar.setTitleTextColor(getResources().getColor(R.color.white)); + + } +} diff --git a/app/src/main/java/com/hi/music/player/ui/ProfileFragment.java b/app/src/main/java/com/hi/music/player/ui/ProfileFragment.java new file mode 100644 index 0000000..c04551e --- /dev/null +++ b/app/src/main/java/com/hi/music/player/ui/ProfileFragment.java @@ -0,0 +1,27 @@ +package com.hi.music.player.ui; + +import android.os.Bundle; + +import androidx.fragment.app.Fragment; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.hi.music.player.R; +import com.hi.music.player.databinding.FragmentProfileBinding; + + +public class ProfileFragment extends BaseFragment { + + + @Override + protected FragmentProfileBinding getFragmentVb() { + return FragmentProfileBinding.inflate(getLayoutInflater()); + } + + @Override + protected void initView() { + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hi/music/player/ui/SearchFragment.java b/app/src/main/java/com/hi/music/player/ui/SearchFragment.java new file mode 100644 index 0000000..98876e1 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/ui/SearchFragment.java @@ -0,0 +1,27 @@ +package com.hi.music.player.ui; + +import android.os.Bundle; + +import androidx.fragment.app.Fragment; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.hi.music.player.R; +import com.hi.music.player.databinding.FragmentSearchBinding; + + +public class SearchFragment extends BaseFragment { + + + @Override + protected FragmentSearchBinding getFragmentVb() { + return FragmentSearchBinding.inflate(getLayoutInflater()); + } + + @Override + protected void initView() { + + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/home_background.xml b/app/src/main/res/drawable/home_background.xml new file mode 100644 index 0000000..a4b40ce --- /dev/null +++ b/app/src/main/res/drawable/home_background.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/home_select.xml b/app/src/main/res/drawable/home_select.xml new file mode 100644 index 0000000..ea2f40c --- /dev/null +++ b/app/src/main/res/drawable/home_select.xml @@ -0,0 +1,19 @@ + + + + + + + diff --git a/app/src/main/res/drawable/home_unselect.xml b/app/src/main/res/drawable/home_unselect.xml new file mode 100644 index 0000000..fb55cbd --- /dev/null +++ b/app/src/main/res/drawable/home_unselect.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/drawable/ic_menu_hamburger.xml b/app/src/main/res/drawable/ic_menu_hamburger.xml new file mode 100644 index 0000000..05148ad --- /dev/null +++ b/app/src/main/res/drawable/ic_menu_hamburger.xml @@ -0,0 +1,12 @@ + + + diff --git a/app/src/main/res/drawable/profile_select.xml b/app/src/main/res/drawable/profile_select.xml new file mode 100644 index 0000000..bb4bdb1 --- /dev/null +++ b/app/src/main/res/drawable/profile_select.xml @@ -0,0 +1,14 @@ + + + + diff --git a/app/src/main/res/drawable/profile_unselect.xml b/app/src/main/res/drawable/profile_unselect.xml new file mode 100644 index 0000000..3c33911 --- /dev/null +++ b/app/src/main/res/drawable/profile_unselect.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/right_arrow.xml b/app/src/main/res/drawable/right_arrow.xml new file mode 100644 index 0000000..046579e --- /dev/null +++ b/app/src/main/res/drawable/right_arrow.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/drawable/round_rectangle.xml b/app/src/main/res/drawable/round_rectangle.xml new file mode 100644 index 0000000..1c13032 --- /dev/null +++ b/app/src/main/res/drawable/round_rectangle.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/rounded_rectangle_tab_layout.xml b/app/src/main/res/drawable/rounded_rectangle_tab_layout.xml new file mode 100644 index 0000000..45e448d --- /dev/null +++ b/app/src/main/res/drawable/rounded_rectangle_tab_layout.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/search_select.xml b/app/src/main/res/drawable/search_select.xml new file mode 100644 index 0000000..10747da --- /dev/null +++ b/app/src/main/res/drawable/search_select.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/drawable/search_unselect.xml b/app/src/main/res/drawable/search_unselect.xml new file mode 100644 index 0000000..45e09f5 --- /dev/null +++ b/app/src/main/res/drawable/search_unselect.xml @@ -0,0 +1,15 @@ + + + + diff --git a/app/src/main/res/layout/activity_home.xml b/app/src/main/res/layout/activity_home.xml new file mode 100644 index 0000000..ba5cfbe --- /dev/null +++ b/app/src/main/res/layout/activity_home.xml @@ -0,0 +1,24 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml new file mode 100644 index 0000000..7fd70bb --- /dev/null +++ b/app/src/main/res/layout/fragment_home.xml @@ -0,0 +1,204 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_profile.xml b/app/src/main/res/layout/fragment_profile.xml new file mode 100644 index 0000000..7721eef --- /dev/null +++ b/app/src/main/res/layout/fragment_profile.xml @@ -0,0 +1,179 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml new file mode 100644 index 0000000..6e978de --- /dev/null +++ b/app/src/main/res/layout/fragment_search.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/home_tab_custom.xml b/app/src/main/res/layout/home_tab_custom.xml new file mode 100644 index 0000000..7740f86 --- /dev/null +++ b/app/src/main/res/layout/home_tab_custom.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cf9489a..35c0625 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,12 @@ MusicApp + + Hello blank fragment + Song Of The Day + Recently Played + Top Artists + Trending in Shorts + New Releases + Musicoo + Profile \ No newline at end of file