Android 写排行榜,顶部前三

ops/2024/12/21 19:30:24/

activity_step_rank.xml

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/fragment_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> </FrameLayout>

fragment_step_rank.xml

<?xml version="1.0" encoding="UTF-8"?>-<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"><androidx.recyclerview.widget.RecyclerView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/recycler_view"/></LinearLayout>

item_step_rank.xml

<?xml version="1.0" encoding="UTF-8"?>-<LinearLayout android:layout_height="60dp" android:layout_marginBottom="6dp" android:layout_width="match_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:textColor="#333333" android:layout_gravity="center_vertical" android:paddingRight="12dp" android:paddingLeft="12dp" android:id="@+id/tv_rank"/><View android:layout_height="match_parent" android:layout_width="match_parent" android:background="#EDEDED"/></LinearLayout>

item_step_rank_top.xml

<?xml version="1.0" encoding="UTF-8"?>-<LinearLayout android:orientation="horizontal" android:layout_height="200dp" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"><!--第二名-->-<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"><View android:layout_height="80dp" android:layout_width="80dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:background="#EDEDED"/></androidx.constraintlayout.widget.ConstraintLayout><!--第一名-->-<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"><View android:layout_height="80dp" android:layout_width="80dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:background="#EDEDED"/></androidx.constraintlayout.widget.ConstraintLayout><!--第三名-->-<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"><View android:layout_height="80dp" android:layout_width="80dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:background="#EDEDED"/></androidx.constraintlayout.widget.ConstraintLayout></LinearLayout>

BaseHolder.java

package com.example.myapplication2024.rank;import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.collection.ArrayMap;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;/*** RecyclerView ViewHolder*/
public class BaseHolder extends RecyclerView.ViewHolder {private final Context mContext;private final SparseArray<View> mSparseArray;public BaseHolder(@NonNull View itemView) {super(itemView);mContext = itemView.getContext();mSparseArray = new SparseArray<>();}public BaseHolder(@LayoutRes int resource, @NonNull ViewGroup parent) {this(LayoutInflater.from(parent.getContext()).inflate(resource, parent, false));}/*** 设置 itemView最小高度*/public BaseHolder setMinHeight(int minHeight) {itemView.setMinimumHeight(minHeight);return this;}public Context getContext() {return mContext;}@SuppressWarnings("all")public <T extends View> T findViewById(@IdRes int id) {View view = mSparseArray.get(id);if (view == null) {view = itemView.findViewById(id);mSparseArray.put(id, view);}return ((T) view);}public <T extends View> T findViewById(@IdRes int id, boolean visibility) {T view = findViewById(id);view.setVisibility(visibility ? View.VISIBLE : View.GONE);return view;}public <T extends View> T findViewById(@IdRes int id, int visibility) {T view = findViewById(id);view.setVisibility(visibility);return view;}public void setText(@IdRes int id, CharSequence text) {TextView textView = findViewById(id);textView.setText(text);}public void setText(@IdRes int id, int textResOrNum) {TextView textView = findViewById(id);try {textView.setText(getContext().getString(textResOrNum));} catch (Exception e) {textView.setText(String.valueOf(textResOrNum));}}public void setHint(@IdRes int id, CharSequence text) {TextView textView = findViewById(id);textView.setHint(text);}public void setHint(@IdRes int id, int textResOrNum) {TextView textView = findViewById(id);try {textView.setHint(getContext().getString(textResOrNum));} catch (Exception e) {textView.setHint(String.valueOf(textResOrNum));}}public ImageView findImageById(@IdRes int imageViewId) {return findViewById(imageViewId);}public ImageView findImageById(@IdRes int imageViewId, boolean visibility) {return findViewById(imageViewId, visibility);}public ImageView findImageById(@IdRes int imageViewId, int visibility) {return findViewById(imageViewId, visibility);}public void setVisibility(int id, int visibility) {findViewById(id).setVisibility(visibility);}public void setVisibility(int id, boolean visibility) {findViewById(id).setVisibility(visibility ? View.VISIBLE : View.GONE);}public void setImageResource(@IdRes int imageViewId, @DrawableRes int drawableRes) {ImageView imageView = findViewById(imageViewId);imageView.setImageResource(drawableRes);}private final ArrayMap<String, Drawable> mCircleDrawableMap = new ArrayMap<>();/*** 获取圆形的Drawable** @param size  宽高* @param color 颜色*/public Drawable getCircleDrawable(int size, int color) {String key = String.valueOf(size) + color;Drawable drawable = mCircleDrawableMap.get(key);if (drawable == null) {GradientDrawable gradientDrawable = new GradientDrawable();gradientDrawable.setShape(GradientDrawable.OVAL);if (size > 0) {gradientDrawable.setSize(size, size);}gradientDrawable.setColor(color);drawable = gradientDrawable;mCircleDrawableMap.put(key, drawable);}return drawable;}public Drawable getCircleDrawable(int size) {return getCircleDrawable(size, Color.parseColor("#EDEDED"));}public int getColor(@ColorRes int id) {return ContextCompat.getColor(getContext(), id);}}

StepRank.java

package com.example.myapplication2024.rank;/*** 步数数据*/
public class StepRank {
}

StepRankActivity.java

package com.example.myapplication2024.rank;import android.content.Context;
import android.content.Intent;
import android.os.Bundle;import com.example.myapplication2024.R;import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;/*** Activity*/
public class StepRankActivity extends FragmentActivity {public static void start(Context context) {Intent starter = new Intent(context, StepRankActivity.class);context.startActivity(starter);}@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_step_rank);getSupportFragmentManager().beginTransaction().add(R.id.fragment_parent, new StepRankFragment()).commitAllowingStateLoss();}
}

StepRankAdapter.java

package com.example.myapplication2024.rank;import android.view.View;
import android.view.ViewGroup;import com.example.myapplication2024.R;import java.util.ArrayList;
import java.util.List;import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;/*** 步数适配器*/
public class StepRankAdapter extends RecyclerView.Adapter<BaseHolder> {private static final int TYPE_TOP = -8123;private final StepRank[] mTopThreeData = new StepRank[3];private final List<StepRank> mData = new ArrayList<>();public void setData(List<StepRank> data) {mTopThreeData[0] = null;mTopThreeData[1] = null;mTopThreeData[2] = null;mData.clear();if (data != null) {for (int i = 0; i < data.size() && data.size() < 3; i++) {mTopThreeData[i] = data.get(i);}if (data.size() > 3) {mData.addAll(data.subList(3, data.size()));}}notifyDataSetChanged();}public void addAll(List<StepRank> data) {if (data != null) {mData.addAll(data);notifyDataSetChanged();}}@NonNull@Overridepublic BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {if (viewType == TYPE_TOP) {return new BaseHolder(R.layout.item_step_rank_top, parent);}return new BaseHolder(R.layout.item_step_rank, parent);}@Overridepublic void onBindViewHolder(@NonNull BaseHolder holder, int position) {if (getItemViewType(position) == TYPE_TOP) {//前三.通过 holder.findViewById   对应item_step_rank_topreturn;}//其他 对应 item_step_rankholder.setText(R.id.tv_rank,String.valueOf(position+3));}@Overridepublic int getItemViewType(int position) {if (position == 0) {return TYPE_TOP;} else {return -1;}}@Overridepublic int getItemCount() {if (mData.isEmpty()) {for (StepRank topThreeDatum : mTopThreeData) {if (topThreeDatum != null) {return 1;}}return 0;} else {return mData.size() + 1;}}public void attachToRecyclerView(@NonNull RecyclerView recyclerView) {recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));recyclerView.setAdapter(this);}static class StepRankHolder extends RecyclerView.ViewHolder {public StepRankHolder(@NonNull View itemView) {super(itemView);}}
}

StepRankFragment.java

package com.example.myapplication2024.rank;import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.example.myapplication2024.R;import java.util.ArrayList;
import java.util.List;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;/*** Fragment*/
public class StepRankFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_step_rank, container, false);}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);final RecyclerView recyclerView = view.findViewById(R.id.recycler_view);final StepRankAdapter adapter = new StepRankAdapter();adapter.attachToRecyclerView(recyclerView);final List<StepRank> data = new ArrayList<>();for (int i = 0; i < 20; i++) {data.add(new StepRank());}adapter.setData(data);}
}

http://www.ppmy.cn/ops/143838.html

相关文章

进程间通信方式---消息队列(System V IPC)

进程间通信方式—消息队列&#xff08;System V IPC&#xff09; 文章目录 进程间通信方式---消息队列&#xff08;System V IPC&#xff09;消息队列1.消息队列进程间通信原理2.msgget 系统调用3.msgsnd 系统调用4.msgrcv 系统调用5.msgctl 系统调用6.函数使用案例7.实现生产者…

BOE(京东方)亮相世界显示产业创新发展大会 以创新科技全面引领行业风向标

2024年12月19日,世界显示产业创新发展大会在成都举办,来自全球的显示领域企业及行业专家汇聚一堂,共同探讨新一轮产业升级趋势及行业未来发展方向。BOE(京东方)总裁高文宝博士应邀出席大会,并在开幕式发表《屏之物联 聚智创新》主题演讲,引发现场嘉宾和观众强烈共鸣。大会期间,…

一文流:JVM精讲(多图提醒⚠️)

一文流系列是作者苦于技术知识学了-忘了,背了-忘了的苦恼,决心把技术知识的要点一笔笔✍️出来,一图图画出来,一句句讲出来,以求刻在🧠里。 该系列文章会把核心要点提炼出来,以求掌握精髓,至于其他细节,写在文章里,留待后续回忆。 目前进度请查看: :::info https:/…

[图] 遍历 | BFS | DFS

目录 1. 基本概念 2. 图的广度优先遍历&#xff08;BFS&#xff09; 3. 图的深度优先遍历&#xff08;DFS&#xff09; 4. 非连通图的遍历 1. 基本概念 给定一个图G和其中任意一个顶点v0&#xff0c;从v0出发&#xff0c;沿着图中各边访问图中的所有顶点&#xff0c;且每个…

【练习Day17】寻找第 K 大

链接&#xff1a;寻找第K大_牛客题霸_牛客网 方法&#xff1a;快排二分查找&#xff08;推荐使用&#xff09; 知识点&#xff1a;分治 分治即“分而治之”&#xff0c;“分”指的是将一个大而复杂的问题划分成多个性质相同但是规模更小的子问题&#xff0c;子问题继续按照这…

netfilter简介及流程图

Netfilter 是 Linux 内核中用于网络包过滤和操作的框架&#xff0c;由 Rusty Russell 于1998年创立&#xff0c;旨在改进旧的 ipchains 和 ipfwadm 实现。它采用模块化设计&#xff0c;具有良好可扩展性&#xff0c;并在2000年3月合并进Linux 2.3.x内核版本。 Netfilter的主要…

ElasticSearch学习7

SpringBoot整合文档相关的操作 1、文档的添加 // 测试添加文档(先创建一个User实体类&#xff0c;添加fastjson依赖) Test public void testAddDocument() throws IOException { // 创建一个User对象 User liuyou new User("liuyou", 18); // 创建请求 …

电力场景电力部件热点区域检测数据集VOC+YOLO格式183张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;183 标注数量(xml文件个数)&#xff1a;183 标注数量(txt文件个数)&#xff1a;183 标注…