The establishment and use of Android studio APP development ListView control

ListView control

ListView is a very important part of the Android APP development, showing a list of information, very common in APP, such as contact information in the phone book, and QQ micro-channel in the buddy list information, are made with a ListView.

The idea to create a ListView

  1. It is easy to imagine, build a ListView, we must first know in which interface display ListView view , which is to create the first layout layout file .
  2. QQ friends and contacts information are presented with a list, each message is a control , the layout of the controls is the need to design, so there needs to file another layout layout design controls to display user information . That is the view
  3. QQ friends and contacts is an object , so it is necessary to establish a class , here named UserInfo , ie the user class information, store the user's name, age, avatar, etc.
  4. Now have a view and data, to do is to associate data and view it . In the Android development in each must bind a ListView Adapter . Adapter implements bind and view data
  5. General QQ friends or contacts more than one , so they need an array to store the information, but will not work with traditional arrays, because we did not start to know how many friends or contacts, often need to add friends and contacts . So here with ArrayList class to define a variable length array .

Steps to create a ListView

Create a view interface

Activity first create a new file, named ListViewDemoActivity (customizable). code show as below:

public class ListViewDemoActivity extends Activity {
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_listview_demo); //相关联的布局文件中有 ListView控件
	}
}

Then create corresponding layout document, as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话本"/>
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

ListView view this document on the establishment of good, but need to set the jump event jump from other interfaces to this interface, the code will not be repeated here.

The establishment of user information UserInfo class

QQ friends or contacts an object, a need to abstract class, because of the type as.
New UserInfo class code as follows:

import java.io.Serializable;

public class UserInfo implements Serializable {
    private String mUserName;
    private int mAge;
    private String mAvatarUrl;
    private float mWeight;
    public UserInfo(String mUserName, int mAge) {
        this.mUserName = mUserName;
        this.mAge = mAge;
    }

    public String getmUserName() {
        return mUserName;
    }

    public int getmAge() {
        return mAge;
    }

    public void setmUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public void setmAge(int mAge) {
        this.mAge = mAge;
    }

    public String getAvatarUrl() {
        return mAvatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        mAvatarUrl = avatarUrl;
    }

    public float getWeight() {
        return mWeight;
    }

    public void setWeight(float weight) {
        mWeight = weight;
    }
}

Establishment of category Adapter views and data binding

Now a need to create and view data class is used to bind,
to establish a PhoneBookAdapter class, as follows:

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

    public class PhoneBookAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInfalter;
    // 10 中定义 用ArrayList来存储数据,新增数据
    private List<UserInfo> mUserInfos = new ArrayList<>();

    //构造函数,初始化
    public PhoneBookAdapter(Context context, List<UserInfo> userInfos){
        mContext = context;
        mUserInfos = userInfos;
        mLayoutInfalter =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // 有多少条数据
        return mUserInfos.size();
    }

    @Override
    public Object getItem(int position) {  //position就是这条数据的位置
        //返回某一条数据,
        return mUserInfos.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    //返回某一条数据的视图,因为每一条数据都会有自己的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //返回一个视图,convertView 就是找到要绑定的视图,LayoutInfalter的主要功能就是解析View
        // 将Layout从xml文件从解析出来,解析成View,一个视图
        convertView=mLayoutInfalter.inflate(R.layout.item_phone_book_friend,null);

        //获取控件,从视图中获取了控件
        TextView nameTextView= (TextView) convertView.findViewById(R.id.name_text_view);
        TextView ageTextView = (TextView) convertView.findViewById(R.id.age_text_view);
        ImageView avatarimageView = (ImageView) convertView.findViewById(R.id.avatar_image_view);
        //和数据之间进行绑定
        nameTextView.setText(mUserInfos.get(position).getmUserName()); //设置文本
        ageTextView.setText(String.valueOf(mUserInfos.get(position).getmAge()));
        avatarimageView.setImageResource(R.drawable.ic_launcher_foreground);

        return convertView;  //返回一个视图
    }
}

Comment which explains the code has to be obtained by this class, user information, number, and user information and the control is bound.

Each user defined display information control:

In ListView, the user's information list display, each piece of information is equivalent to a control, and you can add click events. So it is necessary to create a layout file to design the layout of the control. Personalization is designed mainly in this section.

<?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="48dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/avatar_image_view"
        android:src="@drawable/ic_launcher_background"
        android:layout_width="48dp"
        android:layout_height="48dp"/>

    <TextView
        android:id="@+id/name_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_toRightOf="@+id/avatar_image_view"
        android:text="@string/app_name"/>
    <TextView
        android:id="@+id/age_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name_text_view"
        android:layout_toRightOf="@+id/avatar_image_view"
        android:text="4岁"/>

</RelativeLayout>

In the ListView ListView and bind Adapter and set the click event

User class information, and the data bound control PhoneBookDemoAdapter are ready to do now is to bind the ListView and Adapter, and add user data, also set the click event for each control.
code show as below:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
// 这个ListViewDemoActivity是由 MainActivity点击进入,也就是ListView展示的界面
public class ListViewDemoActivity extends Activity {
    private ListView mphoneBookListView;
    private List<UserInfo> userInfos; //声明一个List的全局变量 userInfo
    //UserInfo 就是自定义的一个类,用来存储用户的信息,User iformationn
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_demo); //相关联的布局文件中有 ListView控件

         mphoneBookListView = (ListView) findViewById(R.id.list_view); //取得这个ListView控件


         // 10  向userInfos中添加元素,在这里添加用户的信息数据
        userInfos = new ArrayList<>();
        userInfos.add(new UserInfo("刘小明",16));
        userInfos.add(new UserInfo("刘要明",16));
        userInfos.add(new UserInfo("刘叁明",16));
        userInfos.add(new UserInfo("刘时明",16));
        userInfos.add(new UserInfo("刘瑟明",16));

        //设置这个ListVIew的Adapter , PhoneBookAdapter进行了视图和数据的绑定
        //在引用PhoneBookAdapter类定义对象时初始化,用上面定义的ArrayList初始化定义
        PhoneBookAdapter phoneBookAdapter= new PhoneBookAdapter(ListViewDemoActivity.this, userInfos);
         mphoneBookListView.setAdapter(phoneBookAdapter);

         //设置点击事件
        mphoneBookListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewDemoActivity.this,userInfos.get(position).getmUserName()+"被我点击了",Toast.LENGTH_LONG).show();

            }
        });
    }
}

After running effect is shown:
ListView

Copyright, reproduced indicate the source

Published 50 original articles · won praise 3 · Views 5180

Guess you like

Origin blog.csdn.net/Ace_bb/article/details/104066710