Android 写排行榜,顶部前三

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);
    }
}
相关推荐
千里马学框架1 小时前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 小时前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 小时前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo4 小时前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077005 小时前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼5 小时前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone6 小时前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen6 小时前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone6 小时前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui