UI界面的编写
This commit is contained in:
parent
8e58ca7d66
commit
ffb175ff78
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<DrawerCardsAdapter.VH> {
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onClick(DrawerCardItem item);
|
||||
}
|
||||
|
||||
private final List<DrawerCardItem> items = new ArrayList<>();
|
||||
private final OnItemClickListener onItemClick;
|
||||
|
||||
public DrawerCardsAdapter(OnItemClickListener onItemClick) {
|
||||
this.onItemClick = onItemClick;
|
||||
}
|
||||
|
||||
public void submitList(List<DrawerCardItem> 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<DrawerCardItem> 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 = () -> {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/drawerLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:id="@+id/appBar"
|
||||
android:layout_width="match_parent"
|
||||
|
|
@ -233,4 +238,14 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom" />
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/drawerCards"
|
||||
layout="@layout/drawer_cards_header"
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="start"
|
||||
android:background="@android:color/white" />
|
||||
|
||||
</androidx.drawerlayout.widget.DrawerLayout>
|
||||
|
|
|
|||
27
android-app/app/src/main/res/layout/drawer_cards_header.xml
Normal file
27
android-app/app/src/main/res/layout/drawer_cards_header.xml
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingBottom="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/drawerTitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="快捷入口"
|
||||
android:textColor="#111111"
|
||||
android:textSize="14sp" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/drawerRecyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_weight="1"
|
||||
android:overScrollMode="never" />
|
||||
|
||||
</LinearLayout>
|
||||
63
android-app/app/src/main/res/layout/item_drawer_card.xml
Normal file
63
android-app/app/src/main/res/layout/item_drawer_card.xml
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
app:cardCornerRadius="12dp"
|
||||
app:cardElevation="2dp"
|
||||
app:strokeColor="#14000000"
|
||||
app:strokeWidth="1dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="14dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingEnd="14dp"
|
||||
android:paddingBottom="12dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/icon"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:contentDescription="icon" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="12dp"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="#111111"
|
||||
android:textSize="15sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/subtitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
android:textColor="#666666"
|
||||
android:textSize="12sp" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/chevron"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=">"
|
||||
android:textColor="#999999"
|
||||
android:textSize="16sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
2
android-app/app/src/main/res/menu/drawer_empty.xml
Normal file
2
android-app/app/src/main/res/menu/drawer_empty.xml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" />
|
||||
18
android-app/app/src/main/res/menu/drawer_main.xml
Normal file
18
android-app/app/src/main/res/menu/drawer_main.xml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<group android:checkableBehavior="single">
|
||||
<item
|
||||
android:id="@+id/drawer_settings"
|
||||
android:title="设置" />
|
||||
|
||||
<item
|
||||
android:id="@+id/drawer_placeholder_2"
|
||||
android:title="功能占位 2" />
|
||||
|
||||
<item
|
||||
android:id="@+id/drawer_placeholder_3"
|
||||
android:title="功能占位 3" />
|
||||
</group>
|
||||
|
||||
</menu>
|
||||
Loading…
Reference in New Issue
Block a user