当前位置: 首页 > article >正文

Android 写排行榜,顶部前三

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);
    }

    @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

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

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);
    }
}


http://www.kler.cn/a/441072.html

相关文章:

  • RK3568 opencv播放视频
  • SAP SD学习笔记27 - 请求计划(开票计划)之1 - 定期请求
  • 商密测评题库详解:商用密码应用安全性评估从业人员考核题库详细解析(8)
  • 论文阅读(七):贝叶斯因果表型网络解释遗传变异和生物学知识
  • 【漫话机器学习系列】067.希腊字母(greek letters)-写法、名称、读法和常见用途
  • sem_wait的概念和使用案列
  • 字符2
  • Group FLUX - Summary Essay of the Alpha Phase Problem
  • Next.js流量教程:如何在 Next.js 中添加结构化数据以生成丰富摘要(Rich Snippets)
  • 【现代服务端架构】传统服务器 对比 Serverless
  • 电机控制杂谈(23)——共模电压与轴电流
  • es 开启slowlog
  • UIP协议栈 TCP通信客户端 服务端,UDP单播 广播通信 example
  • 本地部署大模型QPS推理测试
  • sql中case when若条件重复 执行的顺序
  • 召回系统介绍
  • 【Elasticsearch】关键数据类型
  • 20241217使用M6000显卡在WIN10下跑whisper来识别中英文字幕
  • 蜂鸟视图的蜂鸟云开发者中心更新:JS SDK v3.1.8 与 微信小程序 SDK v3.1.8 全新上线!
  • 【mysql】row模式的主从架构中,删除无主键的表可能导致从库“夯住”或产生较大的同步延迟
  • JDK以及JRE
  • 三菱协议以及C#实现
  • 【十进制整数转换为其他进制数——短除形式的贪心算法】
  • 【kubernetes】kubectl get nodes报NotReady
  • iOS和安卓,怎样才能轻松实现文件互传?
  • 爬虫运行中遇到反爬虫策略怎么办