Android手机应用开发(三) | Intent、Bundle的使用以及RecyclerView、ListView的应用

本文详述Android应用开发中的Intent、Bundle在Activity跳转中的使用,以及RecyclerView和ListView的实现,包括布局管理器、点击事件、动画设置。同时探讨了去除标题栏、FloatingActionButton的运用及数据传递方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验目的:

  1. 复习事件处理。
  2. 学习Intent、Bundle在Activity跳转中的应用。
  3. 学习RecyclerView、ListView以及各类适配器的用法。
  4. 学习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>

应该是这个样子

1539499377087

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("海鱼",
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值