实验目的:
- 复习事件处理。
- 学习Intent、Bundle在Activity跳转中的应用。
- 学习RecyclerView、ListView以及各类适配器的用法。
- 学习FloatingActionBar的用法。
去掉标题栏
先来讨论去掉标题栏的问题
打开文件res/values/styles.xml
,添加
<style name="NoTitle" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
打开文件manifests/AndroidManifests.xml
把application
里面的android:theme="@style/AppTheme"
改为android:theme="@style/NoTitle"
如果有多个Activity
,但是只想去掉某个Activity的标题栏怎么办呢?
那就不要改application
的属性,找到需要修改的activity
,改成下面的样子
<activity
android:name=".SecondActivity"
android:theme="@style/NoTitle"></activity
效果在后面有
RecyclerView
概述:
RecyclerView
标准化了ViewHolder
,而且异常的灵活,可以轻松实现ListView
实现不了的样式和功能,通过布局管理器LayoutManager
可控制Item
的布局方式,通过设置Item
操作动画自定义Item
添加和删除的动画,通过设置Item
之间的间隔样式,自定义间隔。
- 设置布局管理器以控制
Item
的布局方式,横向、竖向以及瀑布流方式。 - 可设置
Item
操作的动画(删除或者添加等) - 可设置
Item
的间隔样式(可绘制)
但是关于Item
的点击和长按事件,需要用户自己去实现
使用:
首先在build.gradle(Module:app)
的dependencies
处添加依赖
implementation 'com.android.support:recyclerview-v7:28.0.0'
这里的28.0.0根据
Android
项目的sdk版本来选择
然后在res/layout
创建一个RecyclerView
的单项布局文件activity_single_recyclerview.xml
<--file:activity_single_recyclerview.xml-->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/rv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:background="@drawable/food_icon"
android:gravity="center"
android:text="肉"
android:textColor="@color/colorWhite"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/rv_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:paddingTop="20dp"
android:text="好吃的肉"
android:textColor="@color/colorBlack"
android:textSize="30sp"
app:layout_constraintStart_toEndOf="@id/rv_icon" />
</android.support.constraint.ConstraintLayout>
应该是这个样子
在java/com.example.yourname
,比如我的是java/com.janking.sysuhealth
新建一个数据类Food.java
,用来给RecyclerView
填充数据
这里用了
implements Serializable
,是为了方便后面传递Intent
package com.janking.sysuhealth;
import android.graphics.Color;
import java.io.Serializable;
public class Food implements Serializable {
private String name;
private String category;
private String nutrition;
private Boolean favorite;
private String color;
public Food(String a, String b, String c, String d){
name = a;
category = b;
nutrition = c;
favorite = false;
color = d;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(String category) {
this.category = category;
}
public void setNutrition(String nutrition) {
this.nutrition = nutrition;
}
public void setFavorite(Boolean favorite) {
this.favorite = favorite;
}
public Boolean getFavorite() {
return favorite;
}
public String getCategory() {
return category;
}
public String getName() {
return name;
}
public String getNutrition() {
return nutrition;
}
public String getColor() {
return color;
}
}
接下来新建一个类MyAdapter.java
,用来决定数据信息以及展示的UI
package com.janking.sysuhealth;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import jp.wasabeef.recyclerview.animators.holder.AnimateViewHolder;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Food> mDatas;
private MyAdapter.OnItemClickListener onItemClickListener;
public MyAdapter(){
initData();
}
public void initData()
{
mDatas = new ArrayList<Food>();
mDatas.add(new Food("大豆", "粮食", "蛋白质", "#BB4C3B"));
mDatas.add(new Food("十字花科蔬菜","蔬菜","维生素C", "#C48D30"));
mDatas.add(new Food("牛奶","饮品","钙","#4469B0"));
mDatas.add(new Food("海鱼",