Android development, basic application, recipe development summary

1. Use of listview and GridView

Firstly, the use of these two components requires adapters. We need to customize the adapters. In the customization process, we need to inherit arrayadapter < > (), in which we need to rewrite two important functions, int getPosition() and View getView(). In this function, we need to write the View, that is, add data to the View, At this time, we also need a data source. Here we use the List collection

Use of adapter: fill the data into the data source in the UI component

 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //Assign a value to the layout in the resource
        //Get the current class and use getItem
        food f=getItem(position);
        View view;
        //Set the view corresponding to f
        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);
        //set up
        imageView.setImageResource(f.getFoodImage());
        textView.setText(f.getFoodName());
        textView1.setText(f.getNotMatchFood());
        return view;
    }

Set a click event for each Item

  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Pass the current food object to intent
                food f=Gridfood.get(position);
                Intent intent=new Intent(food_Match.this, FoodDesActivity.class);
                //Pass food object
                intent.putExtra("food",f);
                startActivity(intent);
            }
        });
    }

2. Use of Scrollview

The screen size is different, and then we need to present all the information. We need to use the scroll layout, which is the vertical scroll layout, and the HorizontalScrollView is the horizontal scroll layout

Requirements for use: only one child element can be used, but the child elements of the child element are unrestricted. Here, the child element uses the layout of LinearLayout, and other controls are added to the layout

<?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="beef"
                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="beef"
            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="Food not suitable for eating together"
            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="Olive, leek"
            android:layout_marginLeft="10dp"
            android:textSize="16sp"/>

    </LinearLayout>

</ScrollView>

3. The use of ImageView property scaleType property is used to display pictures

With FIT_ The common feature of the first four is that they all zoom the picture,

CENTER_ The common feature of the first three types is that they are displayed in the CENTER. The CENTER point of the picture will overlap with the CENTER point of ImageView without zooming

ScaleType.MATRIX, matrix mode, can translate and rotate the picture

4. When using layout_ When gravity = "center", there is no centering effect because the length and width of the control are set to match_parent;

5. Use Intent to jump activities and transfer data

             food f=Gridfood.get(position);
                Intent intent=new Intent(food_Match.this, FoodDesActivity.class);
                //Pass food object
                intent.putExtra("food",f);
                startActivity(intent);
        //Get the result returned by the previous page
        Intent intent=getIntent();
        food f=(food) intent.getSerializableExtra("food");
        //Then assign a value
        title1.setText(f.getFoodName());
        title2.setText(f.getFoodName());
        notEat.setText(f.getNotMatchFood());
        food_dec.setText(f.getFoodDec());
        food_picture.setImageResource(f.getFoodImage());

What is passed here is an object whose class needs to implement the Serializable interface

6. Use of viewpager to realize rotation chart

First, we need to add the ViewPager view

<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>

Then we need to prepare a layout to fill the ViewPager. We can change the things in the layout

<?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>

To fill in data, we need an adapter. We need to customize an adapter and need a basic PagerAdapter. We also need to add a data source. Here, we use List < View >. Because the rotation chart needs unlimited playback, the maximum value of Integer is selected for the number of slides, but the number of lists is limited, so we need to take the remainder, so we won't cross the boundary

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() {
        //This determines the number of slides
        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);
    }
}

Then initialize the data source in the main activity and use the adapter

 viewPager=(ViewPager)findViewById(R.id.about_vp);
        share=(TextView)findViewById(R.id.about_tv_share);
        pointLayout=(LinearLayout)findViewById(R.id.about_tv_point);
        //Share settings click event
        share.setOnClickListener(this);
        viewList=new ArrayList<View>();
        //Initialize 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);

We need to switch pictures for a period of time. At this time, we need to use the delay processing of the Handler

 Handler handler=new Handler(){
        //receive messages
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1)
            {
                int currentItem=viewPager.getCurrentItem();
                viewPager.setCurrentItem(currentItem+1);
                //Send the message again to form a close
                handler.sendEmptyMessageDelayed(1,5000);
            }
        }
    };

7. The attribute drawableLeft of textview is used to add a picture to the left of textviewed

8. Use SharePreference to store the state. There will be a boot interface when entering here. There will be a boot interface when entering for the first time, and there will be no boot interface when entering for the second time. At this time, we need to store whether to enter the state for the first time

  SharedPreferences preferences;//Storage status
    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) {
                        //Realize jump
                        intent.setClass(MainActivity.this, GuideActivity.class);
                        editor.putBoolean("isFirst", false);
                        editor.commit();
                    } else {
                        intent.setClass(MainActivity.this, Home_menu.class);
                    }
                    startActivity(intent);
                    finish();
                } else {
                    //send message
                    handler.sendEmptyMessageDelayed(1, 1000);
                    tv.setText(time + "");

                }
            }
        }
    };
 preferences=getSharedPreferences("health",MODE_PRIVATE);
        editor=preferences.edit();//Write data

Keywords: Java Android

Added by schajee on Fri, 14 Jan 2022 08:31:05 +0200