• Android 自定义横向时间轴


    示例:

    一、添加依赖

    1. dependencies {
    2. ***
    3. ***
    4. //添加RecyclerView的依赖包
    5. implementation 'androidx.recyclerview:recyclerview:1.2.1'
    6. }

    二、页面代码

    activity_main.xml:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:gravity="center"
    7. android:orientation="vertical"
    8. tools:context=".MainActivity">
    9. <!--时间轴列表-->
    10. <androidx.recyclerview.widget.RecyclerView
    11. android:id="@+id/rv"
    12. android:layout_width="wrap_content"
    13. android:layout_height="70dp" />
    14. </LinearLayout>

    list_point_cell.xml:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:gravity="center"
    6. android:orientation="vertical">
    7. <TextView
    8. android:id="@+id/item_time"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_marginLeft="10dp"
    12. android:layout_marginRight="10dp"
    13. android:text="time"
    14. android:textSize="12sp" />
    15. <LinearLayout
    16. android:layout_width="match_parent"
    17. android:layout_height="30dp"
    18. android:orientation="horizontal">
    19. <ImageView
    20. android:id="@+id/item_start_img"
    21. android:layout_width="wrap_content"
    22. android:layout_height="wrap_content"
    23. android:layout_weight="1"
    24. android:scaleType="fitXY"
    25. android:src="@mipmap/ic_line"/>
    26. <ImageView
    27. android:id="@+id/item_point_img"
    28. android:layout_width="15dp"
    29. android:layout_height="match_parent"
    30. android:src="@mipmap/ic_unselected_circle"/>
    31. <ImageView
    32. android:id="@+id/item_end_img"
    33. android:layout_width="wrap_content"
    34. android:layout_height="wrap_content"
    35. android:layout_weight="1"
    36. android:scaleType="fitXY"
    37. android:src="@mipmap/ic_line"/>
    38. </LinearLayout>
    39. <TextView
    40. android:id="@+id/item_text"
    41. android:layout_width="wrap_content"
    42. android:layout_height="wrap_content"
    43. android:layout_marginLeft="10dp"
    44. android:layout_marginRight="10dp"
    45. android:text="text"
    46. android:textSize="12sp" />
    47. </LinearLayout>

    三、java代码

    MyRecyclerAdapter.java适配器

    1. package com.example.horiztimeaxis;
    2. import android.content.Context;
    3. import android.content.SharedPreferences;
    4. import android.view.LayoutInflater;
    5. import android.view.View;
    6. import android.view.ViewGroup;
    7. import android.widget.ImageView;
    8. import android.widget.TextView;
    9. import androidx.annotation.NonNull;
    10. import androidx.recyclerview.widget.RecyclerView;
    11. import java.util.HashMap;
    12. import java.util.List;
    13. import static android.content.Context.MODE_PRIVATE;
    14. public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
    15. private OnItemClickListener itemClickListener;// 接口对象
    16. private Context context;// 上下文
    17. private List<HashMap<String,String>> itemList;// 数据集合
    18. private int lastIndex = -1;
    19. public MyRecyclerAdapter(Context context,List<HashMap<String,String>> itemList){
    20. super();
    21. this.context = context;
    22. this.itemList = itemList;
    23. lastIndex = itemList.size()-1;
    24. }
    25. @NonNull
    26. @Override
    27. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    28. // TODO 自动生成的方法存根
    29. LayoutInflater inflater = LayoutInflater.from(context);
    30. View view = inflater.inflate(R.layout.list_point_cell,null);
    31. MyViewHolder holder = new MyViewHolder(view);
    32. return holder;
    33. }
    34. @Override
    35. public void onBindViewHolder(MyViewHolder holder,int position) {
    36. // 设置未选中状态
    37. holder.item_point_img.setImageDrawable(context.getDrawable(R.mipmap.ic_unselected_circle));
    38. holder.item_start_img.setVisibility(View.VISIBLE);
    39. holder.item_end_img.setVisibility(View.VISIBLE);
    40. // 隐藏第一个
    41. if (position==0){
    42. holder.item_start_img.setVisibility(View.INVISIBLE);
    43. }
    44. // 隐藏最后一个
    45. if (position == lastIndex){
    46. holder.item_end_img.setVisibility(View.INVISIBLE);
    47. }
    48. SharedPreferences time_sp = context.getSharedPreferences("TIME_LINE_SP",MODE_PRIVATE);
    49. int index = time_sp.getInt("INDEX_TIME",-1);
    50. if (index==position){
    51. // 设置选中状态
    52. holder.item_point_img.setImageDrawable(context.getDrawable(R.mipmap.ic_selected_circle));
    53. }
    54. HashMap<String, String> hashMap = itemList.get(position);
    55. // 赋值
    56. holder.time.setText(hashMap.get("TIME"));
    57. holder.tv.setText(hashMap.get("TEXT"));
    58. }
    59. @Override
    60. public int getItemCount() {
    61. return itemList.size();
    62. }
    63. protected class MyViewHolder extends RecyclerView.ViewHolder {
    64. private TextView time;
    65. private ImageView item_start_img;
    66. private ImageView item_point_img;
    67. private ImageView item_end_img;
    68. private TextView tv;
    69. /**
    70. * @param itemView
    71. */
    72. public MyViewHolder(View itemView) {
    73. super(itemView);
    74. time = itemView.findViewById(R.id.item_time);
    75. item_start_img = itemView.findViewById(R.id.item_start_img);
    76. item_point_img = itemView.findViewById(R.id.item_point_img);
    77. item_end_img = itemView.findViewById(R.id.item_end_img);
    78. tv = itemView.findViewById(R.id.item_text);
    79. // 设置点击事件
    80. itemView.setOnClickListener(new View.OnClickListener() {
    81. @Override
    82. public void onClick(View v) {
    83. itemClickListener.onItemClick(getAbsoluteAdapterPosition());
    84. }
    85. });
    86. }
    87. }
    88. /**
    89. * 点击接口方法(监听)
    90. * */
    91. public interface OnItemClickListener {
    92. void onItemClick(int position);
    93. }
    94. /**
    95. * 回调函数
    96. * */
    97. public void ItemClickCallBack(OnItemClickListener itemClickListener){
    98. this.itemClickListener = itemClickListener;
    99. }
    100. }
    MainActivity.java
    1. package com.example.horiztimeaxis;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import androidx.recyclerview.widget.LinearLayoutManager;
    4. import androidx.recyclerview.widget.RecyclerView;
    5. import android.content.SharedPreferences;
    6. import android.os.Bundle;
    7. import android.widget.Toast;
    8. import java.util.ArrayList;
    9. import java.util.HashMap;
    10. public class MainActivity extends AppCompatActivity {
    11. private RecyclerView rv;
    12. private MyRecyclerAdapter adapter;
    13. // 时间轴中数据
    14. private ArrayList<HashMap<String,String>> listItem;
    15. @Override
    16. protected void onCreate(Bundle savedInstanceState) {
    17. super.onCreate(savedInstanceState);
    18. setContentView(R.layout.activity_main);
    19. init();
    20. }
    21. /**
    22. * 使用方法
    23. * */
    24. private void init(){
    25. // 数据初始化
    26. initData();
    27. /** ---时间轴--- **/
    28. rv = findViewById(R.id.rv);
    29. //设置布局管理器
    30. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    31. // 竖向布局
    32. //linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    33. // 横向布局
    34. linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    35. // 设置布局
    36. rv.setLayoutManager(linearLayoutManager);
    37. // 设置adpater
    38. adapter = new MyRecyclerAdapter(this,listItem);
    39. adapter.ItemClickCallBack(new MyRecyclerAdapter.OnItemClickListener() {
    40. @Override
    41. public void onItemClick(int position) {
    42. SharedPreferences time_sp = getSharedPreferences("TIME_LINE_SP",MODE_PRIVATE);
    43. SharedPreferences.Editor editor = time_sp.edit();
    44. editor.putInt("INDEX_TIME",position);
    45. editor.commit();
    46. // 刷新
    47. adapter.notifyDataSetChanged();
    48. // 选中内容
    49. HashMap<String, String> map = listItem.get(position);
    50. Toast.makeText(getApplicationContext(),map.get("TIME")+map.get("TEXT"),Toast.LENGTH_SHORT).show();
    51. }
    52. });
    53. rv.setAdapter(adapter);
    54. }
    55. // 初始化显示的数据
    56. private void initData(){
    57. /*在数组中存放数据*/
    58. listItem = new ArrayList<HashMap<String, String>>();
    59. HashMap<String, String> map1 = new HashMap<String, String>(20);
    60. HashMap<String, String> map2 = new HashMap<String, String>(20);
    61. HashMap<String, String> map3 = new HashMap<String, String>(20);
    62. HashMap<String, String> map4 = new HashMap<String, String>(20);
    63. HashMap<String, String> map5 = new HashMap<String, String>(20);
    64. HashMap<String, String> map6 = new HashMap<String, String>(20);
    65. map1.put("TIME", "2023-8-3");
    66. map1.put("TEXT", "土地尚未开工");
    67. listItem.add(map1);
    68. map2.put("TIME", "2023-8-27");
    69. map2.put("TEXT", "夯实地基");
    70. listItem.add(map2);
    71. map3.put("TIME", "2023-9-4");
    72. map3.put("TEXT", "浇筑地梁");
    73. listItem.add(map3);
    74. map4.put("TIME", "2023-9-20");
    75. map4.put("TEXT", "主体砌筑");
    76. listItem.add(map4);
    77. map5.put("TIME", "2023-10-9");
    78. map5.put("TEXT", "封顶");
    79. listItem.add(map5);
    80. map6.put("TIME", "2023-11-10");
    81. map6.put("TEXT", "内部装修");
    82. listItem.add(map6);
    83. }
    84. }

  • 相关阅读:
    完美解决 RabbitMQ可视化界面Overview不显示折线图和队列不显示Messages
    【含泪提速!】一文全解相似度算法、跟踪算法在各个AI场景的应用(附代码)
    windows10 python pip安装django并创建demo
    8/7 牛客6+div2D+倍增lca
    重构:在新底座之上让应用重生
    RabbitMQ——入门介绍
    ts中的元组是什么有什么用
    生成可执行jar
    漏洞分析|Adobe ColdFusion WDDX 序列化漏洞利用
    编写java程序模拟简单的计算器
  • 原文地址:https://blog.csdn.net/qq_19688207/article/details/133761166