1. 涉及到的技术点
- RecyclerView列表控件的使用
- Adapter适配器的使用
2. 代码实现过程
- 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>
- 适配器需要的布局文件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>
- 适配器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);
}
}
- 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. 运行效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/10b2bc061f874320ab54ee660846c1ae.png)