activity_step_rank.xml
c
<?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
c
<?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
c
<?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
c
<?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
c
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
c
package com.example.myapplication2024.rank;
/**
* 步数数据
*/
public class StepRank {
}
StepRankActivity.java
c
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);
}
@Override
protected 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
c
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
@Override
public 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);
}
@Override
public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
if (getItemViewType(position) == TYPE_TOP) {
//前三.通过 holder.findViewById 对应item_step_rank_top
return;
}
//其他 对应 item_step_rank
holder.setText(R.id.tv_rank,String.valueOf(position+3));
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_TOP;
} else {
return -1;
}
}
@Override
public 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
c
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
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_step_rank, container, false);
}
@Override
public 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);
}
}