Android开发基础应用,食谱开发总结

1.ListView和GridView的使用

首先这两个组件的使用,是需要适配器的,我们需要去自定义适配器,在自定义的过程中,要继承ArrayAdapter<>(),其中需要重写两个重要的函数,int getPosition()和View getView(),在这个函数当中我们需要对对View进行编写,就是View中加入数据,这个时候我们也需要数据源,这里用的是List集合

    适配器的使用:将数据填充到UI组件当中的数据源中

 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //对资源里的布局进行赋值
        //获取当前类,使用getItem
        food f=getItem(position);
        View view;
        //对f对应的view进行设置
        if(convertView==null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        }
        else
        {
            view=convertView;
        }
        ImageView imageView=(ImageView) view.findViewById(R.id.food_picture);
        TextView textView=(TextView) view.findViewById(R.id.food_name);
        TextView textView1=(TextView) view.findViewById(R.id.text_nomatch);
        //设置
        imageView.setImageResource(f.getFoodImage());
        textView.setText(f.getFoodName());
        textView1.setText(f.getNotMatchFood());
        return view;
    }

对其中的每个Item设置点击事件

  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //将当前的food对象传递给intent
                food f=Gridfood.get(position);
                Intent intent=new Intent(food_Match.this, FoodDesActivity.class);
                //传递food对象
                intent.putExtra("food",f);
                startActivity(intent);
            }
        });
    }

2.ScrollView的使用

屏幕的大小是有所不同的,然后我们需要将信息都能呈现出来,我们需要用到滚动布局,这个是垂直滚动布局,HorizontalScrollView是水平的滚动布局

使用的要求:子元素只能用一个,但是子元素的子元素是不受限制的,这里子元素用的是LinearLayout的布局,在布局中加入其他的控件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context="foodgrid.FoodDesActivity">
    <LinearLayout
        android:id="@+id/fooddesc_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RelativeLayout
            android:background="@color/purple_700"
            android:layout_width="match_parent"
            android:layout_height="60dp">
            <ImageView
                android:id="@+id/food_back"
                android:layout_width="70dp"
                android:layout_height="60dp"
               android:src="@mipmap/fanhui"
                android:layout_marginLeft="3dp"
                android:layout_centerVertical="true"/>
            <TextView
                android:id="@+id/food_title1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="牛肉"
                android:textColor="@color/teal_200"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_centerInParent="true"/>
        </RelativeLayout>
        <ImageView
            android:id="@+id/food_picture"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:src="@mipmap/niu"
            android:layout_margin="20sp"
            android:scaleType="fitXY"
            />
        <TextView
            android:id="@+id/food_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="牛肉"
            android:textSize="30sp"
            android:textColor="#00838F"
            android:textStyle="bold"
            android:layout_marginTop="30dp"
            android:layout_gravity="center"/>
        <TextView
            android:id="@+id/food_dec"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="111"
            android:layout_margin="20dp"
            android:textSize="16sp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#6CC62C"
            android:text="不宜一起食用的食物"
            android:textSize="20sp"
            android:textStyle="bold"
            android:padding="10dp"/>
        <TextView
            android:id="@+id/food_not"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="橄榄菜,韭菜"
            android:layout_marginLeft="10dp"
            android:textSize="16sp"/>

    </LinearLayout>

</ScrollView>

3.ImageView的属性scaleType属性的使用,用于如何显示图片

FIT_开头的4种,它们的共同点是都会对图片进行缩放,

CENTER_开头的3种,它们的共同点是居中显示,图片的中心点会与ImageView的中心点重叠,不使用缩放

ScaleType.MATRIX,矩阵模式,可以对图片进行平移旋转

4.在使用layout_gravity="center"的时候,没有居中效果,是因为控件的长宽有的设置为match_parent;

5.使用Intent进行活动的跳转和传递数据

             food f=Gridfood.get(position);
                Intent intent=new Intent(food_Match.this, FoodDesActivity.class);
                //传递food对象
                intent.putExtra("food",f);
                startActivity(intent);
        //得到上一级页面返回的结果
        Intent intent=getIntent();
        food f=(food) intent.getSerializableExtra("food");
        //然后赋值
        title1.setText(f.getFoodName());
        title2.setText(f.getFoodName());
        notEat.setText(f.getNotMatchFood());
        food_dec.setText(f.getFoodDec());
        food_picture.setImageResource(f.getFoodImage());

这里传递的是对象,该对象的类要实现序列化的接口Serializable

6.ViewPager的使用 实现轮番图

      首先我们要加入ViewPager视图

<LinearLayout 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=".AboutActivity">
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/about_vp"
        android:layout_width="match_parent"
        android:layout_height="230dp">
    </androidx.viewpager.widget.ViewPager>

然后我们需要准备一个布局是来填充ViewPager的,我们改变这个布局里面的东西就可以了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="230dp"
    android:id="@+id/about_item">
    <ImageView
        android:id="@+id/about_pic"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:src="@mipmap/tomato"
        android:scaleType="centerCrop"/>

</RelativeLayout>

  填充数据,我们是需要适配器的,我们需要自定义一个适配器,需要基础PagerAdapter,我们还有加入数据源,这里用的是List<View>,因为轮番图,这里需要无限的播放,所以滑动的次数选择Integer的最大值,但是List的数目是限定的,所以我们需要取取余,这样不会越界

package com.example.healthydietapp;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class AboutPaper extends PagerAdapter {
    List<View>listView;
    @Override
    public int getCount() {
        //这个决定滑动的次数
        return Integer.MAX_VALUE;
    }

    public AboutPaper(List<View> listView) {
        this.listView = listView;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
       View view=listView.get(position%listView.size());
       container.addView(view);
       return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View view=listView.get(position%listView.size());
        container.removeView(view);
    }
}

然后在主活动中去初始化数据源,然后使用适配器

 viewPager=(ViewPager)findViewById(R.id.about_vp);
        share=(TextView)findViewById(R.id.about_tv_share);
        pointLayout=(LinearLayout)findViewById(R.id.about_tv_point);
        //分享设置点击事件
        share.setOnClickListener(this);
        viewList=new ArrayList<View>();
        //初始化viewList
        for(int i=0;i<picId.length;i++)
        {
            View view= LayoutInflater.from(this).inflate(R.layout.about_item,null);
            ImageView iv=view.findViewById(R.id.about_pic);
            iv.setImageResource(picId[i]);
            viewList.add(view);
        }
        AboutPaper aboutPaper=new AboutPaper(viewList);
        viewPager.setAdapter(aboutPaper);

我们需要过一段时间去切换图片,这个时候我们需要使用Handler的延迟处理

 Handler handler=new Handler(){
        //接收消息
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1)
            {
                int currentItem=viewPager.getCurrentItem();
                viewPager.setCurrentItem(currentItem+1);
                //再次发送消息,形成闭合
                handler.sendEmptyMessageDelayed(1,5000);
            }
        }
    };

7.TextView的属性drawableLeft的使用,是在TextViewd的左边加入一个图片

8.使用SharePreference来存储状态,在这里进去会有一个引导界面,在第一次进去的时候有引导界面,第二次进去就没有引导界面了,这个时候我们需要来存储是否第一次进入的的状态

  SharedPreferences preferences;//存储状态
    SharedPreferences.Editor editor;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                time--;
                if (time == 0) {
                    boolean isFirst = preferences.getBoolean("isFirst", true);
                    Intent intent = new Intent();
                    if (isFirst) {
                        //实现跳转
                        intent.setClass(MainActivity.this, GuideActivity.class);
                        editor.putBoolean("isFirst", false);
                        editor.commit();
                    } else {
                        intent.setClass(MainActivity.this, Home_menu.class);
                    }
                    startActivity(intent);
                    finish();
                } else {
                    //发送消息
                    handler.sendEmptyMessageDelayed(1, 1000);
                    tv.setText(time + "");

                }
            }
        }
    };
 preferences=getSharedPreferences("health",MODE_PRIVATE);
        editor=preferences.edit();//写入数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值