From ffb175ff780ed25e71c08a96feac60b542a9ead7 Mon Sep 17 00:00:00 2001 From: ShiQi <3572915148@qq.com> Date: Fri, 19 Dec 2025 17:54:33 +0800 Subject: [PATCH] =?UTF-8?q?UI=E7=95=8C=E9=9D=A2=E7=9A=84=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/livestreaming/DrawerCardItem.java | 45 +++++++ .../livestreaming/DrawerCardsAdapter.java | 72 +++++++++++ .../example/livestreaming/MainActivity.java | 115 ++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 19 ++- .../main/res/layout/drawer_cards_header.xml | 27 ++++ .../src/main/res/layout/item_drawer_card.xml | 63 ++++++++++ .../app/src/main/res/menu/drawer_empty.xml | 2 + .../app/src/main/res/menu/drawer_main.xml | 18 +++ 8 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 android-app/app/src/main/java/com/example/livestreaming/DrawerCardItem.java create mode 100644 android-app/app/src/main/java/com/example/livestreaming/DrawerCardsAdapter.java create mode 100644 android-app/app/src/main/res/layout/drawer_cards_header.xml create mode 100644 android-app/app/src/main/res/layout/item_drawer_card.xml create mode 100644 android-app/app/src/main/res/menu/drawer_empty.xml create mode 100644 android-app/app/src/main/res/menu/drawer_main.xml diff --git a/android-app/app/src/main/java/com/example/livestreaming/DrawerCardItem.java b/android-app/app/src/main/java/com/example/livestreaming/DrawerCardItem.java new file mode 100644 index 00000000..0a9586a5 --- /dev/null +++ b/android-app/app/src/main/java/com/example/livestreaming/DrawerCardItem.java @@ -0,0 +1,45 @@ +package com.example.livestreaming; + +public class DrawerCardItem { + + public static final int ACTION_PROFILE = 1; + public static final int ACTION_MESSAGES = 2; + public static final int ACTION_MY_FRIENDS = 3; + public static final int ACTION_FISH_POND = 4; + public static final int ACTION_FOLLOWING = 5; + public static final int ACTION_FANS = 6; + public static final int ACTION_LIKES = 7; + public static final int ACTION_HISTORY = 8; + public static final int ACTION_SEARCH = 9; + public static final int ACTION_SETTINGS = 10; + public static final int ACTION_HELP = 11; + public static final int ACTION_ABOUT = 12; + + private final int action; + private final String title; + private final String subtitle; + private final int iconRes; + + public DrawerCardItem(int action, String title, String subtitle, int iconRes) { + this.action = action; + this.title = title; + this.subtitle = subtitle; + this.iconRes = iconRes; + } + + public int getAction() { + return action; + } + + public String getTitle() { + return title; + } + + public String getSubtitle() { + return subtitle; + } + + public int getIconRes() { + return iconRes; + } +} diff --git a/android-app/app/src/main/java/com/example/livestreaming/DrawerCardsAdapter.java b/android-app/app/src/main/java/com/example/livestreaming/DrawerCardsAdapter.java new file mode 100644 index 00000000..5800da07 --- /dev/null +++ b/android-app/app/src/main/java/com/example/livestreaming/DrawerCardsAdapter.java @@ -0,0 +1,72 @@ +package com.example.livestreaming; + +import android.view.LayoutInflater; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.example.livestreaming.databinding.ItemDrawerCardBinding; + +import java.util.ArrayList; +import java.util.List; + +public class DrawerCardsAdapter extends RecyclerView.Adapter { + + public interface OnItemClickListener { + void onClick(DrawerCardItem item); + } + + private final List items = new ArrayList<>(); + private final OnItemClickListener onItemClick; + + public DrawerCardsAdapter(OnItemClickListener onItemClick) { + this.onItemClick = onItemClick; + } + + public void submitList(List list) { + items.clear(); + if (list != null) items.addAll(list); + notifyDataSetChanged(); + } + + @NonNull + @Override + public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + ItemDrawerCardBinding b = ItemDrawerCardBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false); + return new VH(b, onItemClick); + } + + @Override + public void onBindViewHolder(@NonNull VH holder, int position) { + holder.bind(items.get(position)); + } + + @Override + public int getItemCount() { + return items.size(); + } + + static class VH extends RecyclerView.ViewHolder { + private final ItemDrawerCardBinding binding; + private final OnItemClickListener onItemClick; + + VH(ItemDrawerCardBinding binding, OnItemClickListener onItemClick) { + super(binding.getRoot()); + this.binding = binding; + this.onItemClick = onItemClick; + } + + void bind(DrawerCardItem item) { + if (item == null) return; + binding.title.setText(item.getTitle() != null ? item.getTitle() : ""); + binding.subtitle.setText(item.getSubtitle() != null ? item.getSubtitle() : ""); + if (item.getIconRes() != 0) { + binding.icon.setImageResource(item.getIconRes()); + } + binding.getRoot().setOnClickListener(v -> { + if (onItemClick != null) onItemClick.onClick(item); + }); + } + } +} diff --git a/android-app/app/src/main/java/com/example/livestreaming/MainActivity.java b/android-app/app/src/main/java/com/example/livestreaming/MainActivity.java index c1aa0dec..da676ef5 100644 --- a/android-app/app/src/main/java/com/example/livestreaming/MainActivity.java +++ b/android-app/app/src/main/java/com/example/livestreaming/MainActivity.java @@ -16,6 +16,9 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.view.GravityCompat; +import androidx.drawerlayout.widget.DrawerLayout; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -68,6 +71,96 @@ public class MainActivity extends AppCompatActivity { loadCoverAssetsAsync(); } + private void setupDrawerCards() { + if (binding == null || binding.drawerCards == null) return; + + DrawerCardsAdapter adapter = new DrawerCardsAdapter(item -> { + if (item == null) return; + handleDrawerAction(item); + + DrawerLayout drawerLayout = binding.drawerLayout; + if (drawerLayout != null) { + drawerLayout.closeDrawer(GravityCompat.START); + } + }); + + binding.drawerCards.drawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + binding.drawerCards.drawerRecyclerView.setAdapter(adapter); + + List items = new ArrayList<>(); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_PROFILE, "个人主页", "资料、作品、粉丝", R.drawable.ic_person_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_MESSAGES, "消息", "私信、互动通知", R.drawable.ic_chat_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_MY_FRIENDS, "我的好友", "通讯录与挚友", R.drawable.ic_people_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_FISH_POND, "鱼塘", "附近与社交圈", R.drawable.ic_globe_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_FOLLOWING, "我的关注", "你关注的主播", R.drawable.ic_people_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_FANS, "粉丝", "关注你的人", R.drawable.ic_people_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_LIKES, "获赞", "收到的点赞", R.drawable.ic_heart_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_HISTORY, "观看历史", "最近看过的直播", R.drawable.ic_grid_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_SEARCH, "搜索", "找主播/房间/标签", R.drawable.ic_search_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_SETTINGS, "设置", "账号、隐私、通知", R.drawable.ic_menu_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_HELP, "帮助与反馈", "常见问题与建议", R.drawable.ic_chat_24)); + items.add(new DrawerCardItem(DrawerCardItem.ACTION_ABOUT, "关于", "版本信息与协议", R.drawable.ic_menu_24)); + + adapter.submitList(items); + } + + private void handleDrawerAction(DrawerCardItem item) { + int action = item.getAction(); + if (action == DrawerCardItem.ACTION_PROFILE) { + ProfileActivity.start(this); + finish(); + return; + } + if (action == DrawerCardItem.ACTION_MESSAGES) { + MessagesActivity.start(this); + finish(); + return; + } + if (action == DrawerCardItem.ACTION_MY_FRIENDS) { + startActivity(new Intent(this, MyFriendsActivity.class)); + return; + } + if (action == DrawerCardItem.ACTION_FISH_POND) { + startActivity(new Intent(this, FishPondActivity.class)); + finish(); + return; + } + if (action == DrawerCardItem.ACTION_FOLLOWING) { + FollowingListActivity.start(this); + return; + } + if (action == DrawerCardItem.ACTION_FANS) { + FansListActivity.start(this); + return; + } + if (action == DrawerCardItem.ACTION_LIKES) { + LikesListActivity.start(this); + return; + } + if (action == DrawerCardItem.ACTION_HISTORY) { + WatchHistoryActivity.start(this); + return; + } + if (action == DrawerCardItem.ACTION_SEARCH) { + SearchActivity.start(this); + return; + } + if (action == DrawerCardItem.ACTION_SETTINGS) { + SettingsPageActivity.start(this, ""); + return; + } + if (action == DrawerCardItem.ACTION_HELP) { + SettingsPageActivity.start(this, SettingsPageActivity.PAGE_HELP); + return; + } + if (action == DrawerCardItem.ACTION_ABOUT) { + SettingsPageActivity.start(this, SettingsPageActivity.PAGE_ABOUT); + return; + } + String t = item.getTitle() != null ? item.getTitle() : ""; + Toast.makeText(this, "暂未接入:" + t, Toast.LENGTH_SHORT).show(); + } + private void setupRecyclerView() { adapter = new RoomsAdapter(room -> { if (room == null) return; @@ -90,6 +183,18 @@ public class MainActivity extends AppCompatActivity { private void setupUI() { binding.swipeRefresh.setOnRefreshListener(this::fetchRooms); + setupDrawerCards(); + + binding.menuButton.setOnClickListener(v -> { + DrawerLayout drawerLayout = binding.drawerLayout; + if (drawerLayout == null) return; + if (drawerLayout.isDrawerOpen(GravityCompat.START)) { + drawerLayout.closeDrawer(GravityCompat.START); + } else { + drawerLayout.openDrawer(GravityCompat.START); + } + }); + binding.avatarButton.setOnClickListener(v -> { ProfileActivity.start(this); finish(); @@ -250,6 +355,16 @@ public class MainActivity extends AppCompatActivity { stopPolling(); } + @Override + public void onBackPressed() { + DrawerLayout drawerLayout = binding != null ? binding.drawerLayout : null; + if (drawerLayout != null && drawerLayout.isDrawerOpen(GravityCompat.START)) { + drawerLayout.closeDrawer(GravityCompat.START); + return; + } + super.onBackPressed(); + } + private void startPolling() { stopPolling(); pollRunnable = () -> { diff --git a/android-app/app/src/main/res/layout/activity_main.xml b/android-app/app/src/main/res/layout/activity_main.xml index f10fe3de..be266f43 100644 --- a/android-app/app/src/main/res/layout/activity_main.xml +++ b/android-app/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,14 @@ - + + - + + + + + diff --git a/android-app/app/src/main/res/layout/drawer_cards_header.xml b/android-app/app/src/main/res/layout/drawer_cards_header.xml new file mode 100644 index 00000000..487d4bfb --- /dev/null +++ b/android-app/app/src/main/res/layout/drawer_cards_header.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/android-app/app/src/main/res/layout/item_drawer_card.xml b/android-app/app/src/main/res/layout/item_drawer_card.xml new file mode 100644 index 00000000..83a17872 --- /dev/null +++ b/android-app/app/src/main/res/layout/item_drawer_card.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + diff --git a/android-app/app/src/main/res/menu/drawer_empty.xml b/android-app/app/src/main/res/menu/drawer_empty.xml new file mode 100644 index 00000000..30651475 --- /dev/null +++ b/android-app/app/src/main/res/menu/drawer_empty.xml @@ -0,0 +1,2 @@ + + diff --git a/android-app/app/src/main/res/menu/drawer_main.xml b/android-app/app/src/main/res/menu/drawer_main.xml new file mode 100644 index 00000000..53e7dd2f --- /dev/null +++ b/android-app/app/src/main/res/menu/drawer_main.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + +