基于Android平台开发,在线电影购票系统(十二)选择日期列表

1. 涉及到的技术点

  1. RecyclerView列表控件的使用
  2. Adapter适配器的使用

2. 代码实现过程

  1. activity_select_movie_date.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SelectMovieDateActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_ios_24"
        app:title="上影国际影城(新虹店)"
        app:titleTextColor="@color/white" />


    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/today"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:background="@drawable/text_bg_selected"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="今天3月16日"
            android:textColor="@color/white"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/tomorrow"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/text_bg_normal"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="明天3月17日"
            android:textColor="#333333"
            android:textSize="12sp" />


        <TextView
            android:id="@+id/after_tomorrow"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/text_bg_normal"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="后天3月18日"
            android:textColor="#333333"
            android:textSize="12sp" />

    </androidx.appcompat.widget.LinearLayoutCompat>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/movie_date_list_item" />


</androidx.appcompat.widget.LinearLayoutCompat>
  1. 适配器需要的布局文件movie_date_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/ll_left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10:15"
                android:textColor="#333333"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/end_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="13:27散场"
                android:textColor="#999999"
                android:textSize="10sp" />

        </androidx.appcompat.widget.LinearLayoutCompat>


        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/ll_center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp"
            android:layout_toRightOf="@id/ll_left"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="原版3D"
                android:textColor="#444"
                android:textSize="11sp" />

            <TextView
                android:id="@+id/few_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="8号厅(激光按摩椅)"
                android:textColor="#999999"
                android:textSize="10sp" />

        </androidx.appcompat.widget.LinearLayoutCompat>


        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center">


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="2dp"
                    android:src="@drawable/baseline_currency_yen_24" />

                <TextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="98"
                    android:textColor="#FF6D00" />

            </androidx.appcompat.widget.LinearLayoutCompat>


            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:backgroundTint="@color/purple_200"
                app:cardCornerRadius="2dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="12dp"
                    android:paddingTop="4dp"
                    android:paddingRight="12dp"
                    android:paddingBottom="4dp"
                    android:text="购票"
                    android:textColor="@color/white"
                    android:textSize="11sp" />
            </androidx.cardview.widget.CardView>


        </androidx.appcompat.widget.LinearLayoutCompat>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="10dp"
            android:background="#f5f5f5" />

    </RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
  1. 适配器SelectDateListAdapter.java
public class SelectDateListAdapter extends RecyclerView.Adapter<SelectDateListAdapter.MyHolder> {
    private List<MovieDateInfo> movieDateInfoList = new ArrayList<>();


    public void setMovieDateInfoList(List<MovieDateInfo> movieDateInfoList) {
        this.movieDateInfoList = movieDateInfoList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_date_list_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(MyHolder holder, int position) {
        MovieDateInfo movieDateInfo = movieDateInfoList.get(position);
        holder.price.setText(movieDateInfo.getPrice() + "");
        holder.time.setText(movieDateInfo.getTime());
        holder.type.setText(movieDateInfo.getType());
        holder.few_number.setText(movieDateInfo.getFew_number());
        holder.end_time.setText(movieDateInfo.getEnd_time());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position, movieDateInfo);
            }
        });

    }


    @Override
    public int getItemCount() {
        return movieDateInfoList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        private TextView price;
        private TextView time;
        private TextView type;
        private TextView few_number;
        private TextView end_time;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            price = itemView.findViewById(R.id.price);
            time = itemView.findViewById(R.id.time);
            type = itemView.findViewById(R.id.type);
            few_number = itemView.findViewById(R.id.few_number);
            end_time = itemView.findViewById(R.id.end_time);
        }
    }


    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, MovieDateInfo movieDateInfo);
    }

}
  1. SelectMovieDateActivity.java页面
public class SelectMovieDateActivity extends AppCompatActivity {
    private CinemaInfo cinemaInfo;
    private MovieInfo.SubjectsDTO subjectsDTO;

    private Toolbar toolbar;
    private TextView today;
    private TextView tomorrow;
    private TextView after_tomorrow;
    private RecyclerView recyclerView;

    private SelectDateListAdapter mSelectDateListAdapter;

    private String selectCurrentDate = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_movie_date);
        //获取跳转传值
        cinemaInfo = (CinemaInfo) getIntent().getSerializableExtra("cinemaInfo");
        subjectsDTO = (MovieInfo.SubjectsDTO) getIntent().getSerializableExtra("subjectsDTO");
        //初始化控件
        toolbar = findViewById(R.id.toolbar);
        today = findViewById(R.id.today);
        tomorrow = findViewById(R.id.tomorrow);
        after_tomorrow = findViewById(R.id.after_tomorrow);
        recyclerView = findViewById(R.id.recyclerView);

        //初始化适配器
        mSelectDateListAdapter = new SelectDateListAdapter();
        //设置适配器
        recyclerView.setAdapter(mSelectDateListAdapter);

        //设置监听
        mSelectDateListAdapter.setOnItemClickListener(new SelectDateListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, MovieDateInfo movieDateInfo) {

                Intent intent = new Intent(SelectMovieDateActivity.this, MovieBuyTicketActivity.class);
                intent.putExtra("cinemaInfo", cinemaInfo);
                intent.putExtra("movieDateInfo", movieDateInfo);
                intent.putExtra("subjectsDTO", subjectsDTO);
                intent.putExtra("selectCurrentDate", selectCurrentDate);
                startActivity(intent);
            }
        });

        //设置数据
        mSelectDateListAdapter.setMovieDateInfoList(DataService.getMovieDateList(0));
        if (null != cinemaInfo) {
            toolbar.setTitle(cinemaInfo.getCinema_name());
        }
        //设置数据
        today.setText("今天" + getCurrentData());
        tomorrow.setText("明天" + getNextDate(1));
        after_tomorrow.setText("后天" + getNextDate(2));
        //默认今天选中
        selectCurrentDate = getCurrentData();

        today.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTextColor(0);
                selectCurrentDate = getCurrentData();
                mSelectDateListAdapter.setMovieDateInfoList(DataService.getMovieDateList(0));

            }
        });

        tomorrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTextColor(1);
                selectCurrentDate = getNextDate(1);
                mSelectDateListAdapter.setMovieDateInfoList(DataService.getMovieDateList(1));
            }
        });

        after_tomorrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTextColor(2);
                selectCurrentDate = getNextDate(2);
                mSelectDateListAdapter.setMovieDateInfoList(DataService.getMovieDateList(2));
            }
        });

        //返回
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    /**
     * 获取当前日期
     */
    public static String getCurrentData() {
        SimpleDateFormat forment = new SimpleDateFormat("M月dd");
        return forment.format(new Date());
    }

    /**
     * 获取今天之后的日期
     */
    public static String getNextDate(int distanceDay) {

        SimpleDateFormat df = new SimpleDateFormat("M月dd");
        Date beginDate = new Date();

        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = new Date();
        try {
            endDate = df.parse(df.format(date.getTime()));
        } catch (Exception e) {
        }
        return df.format(endDate);
    }

    private void setTextColor(int position) {
        setNormalColor();
        if (position == 0) {
            today.setBackgroundResource(R.drawable.text_bg_selected);
            today.setTextColor(Color.parseColor("#ffffff"));
        } else if (position == 1) {
            tomorrow.setBackgroundResource(R.drawable.text_bg_selected);
            tomorrow.setTextColor(Color.parseColor("#ffffff"));
        } else if (position == 2) {
            after_tomorrow.setBackgroundResource(R.drawable.text_bg_selected);
            after_tomorrow.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    /**
     * 设置默认颜色
     */
    private void setNormalColor() {
        today.setBackgroundResource(R.drawable.text_bg_normal);
        tomorrow.setBackgroundResource(R.drawable.text_bg_normal);
        after_tomorrow.setBackgroundResource(R.drawable.text_bg_normal);
        today.setTextColor(Color.parseColor("#333333"));
        tomorrow.setTextColor(Color.parseColor("#333333"));
        after_tomorrow.setTextColor(Color.parseColor("#333333"));
    }

}

3. 运行效果图

在这里插入图片描述

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值