ListView的实现步骤
1.单独一行的布局可以如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/icon_image_view"android:layout_width="60dp"android:layout_height="60dp"android:src="@mipmap/ic_launcher_round"/><TextViewandroid:id="@+id/app_name_text_view"android:layout_width="match_parent"android:layout_height="60dp"android:textSize="20sp"android:padding="6dp"android:gravity="center_vertical"android:text="@string/app_name"/></LinearLayout>
2.adapter数据绑定
package com.sina.mythree;import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;import java.util.ArrayList;
import java.util.List;public class AppListActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_app_list);ListView applistview = (ListView)findViewById(R.id.app_list_view);List<String> appNames = new ArrayList<>();appNames.add("QQ");appNames.add("weixin");appNames.add("Sina");applistview.setAdapter(new AppListAdapter(appNames));}//AppListAdapter 设置如下,并需要自己的布局public class AppListAdapter extends BaseAdapter{List<String> mAppNames;public AppListAdapter(List<String> appNames){mAppNames = appNames;}@Overridepublic int getCount() {//有多少条数据return mAppNames.size();}@Overridepublic Object getItem(int position) {//返回获取当前数据位置return mAppNames.get(position);}@Overridepublic long getItemId(int position) {//返回获取当前数据的IDreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//处理VIEW和DATA的适配填充过程//把XML载入JAVALayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);//把layout转换成VIEWconvertView = layoutInflater.inflate(R.layout.item_app_list_view,null);ImageView appIconImageView = (ImageView) convertView.findViewById(R.id.icon_image_view);TextView appNameTextView = (TextView) convertView.findViewById(R.id.app_name_text_view);appNameTextView.setText(mAppNames.get(position));return convertView;}}}
优化方向,在n个数据时候防止滑动卡顿,进行缓存。