欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android ListView实战演练

程序员文章站 2022-04-14 16:49:12
...

源码 [工程文件]:https://gitee.com/lwx001/ListView


ListView 基本使用:


Android ListView实战演练
activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</RelativeLayout>

ListView 实战演练:


Android ListView实战演练
market.xml :

<?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="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:divider="#EE82EE"
        android:dividerHeight="3dp"></ListView>

</RelativeLayout>

list_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_margin="8dp"
        android:background="@drawable/wx" />

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是ListView的Item布局"
        android:textSize="18sp" />
</LinearLayout>

MainActivity.java :

package cn.lwx.listview;

import androidx.appcompat.app.AppCompatActivity;

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;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    //定义需要适配的数据
    private String[] names = {"京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫", "UC浏览器", "微信"};
    //定义图片集合
    private int[] icons = {R.drawable.jd, R.drawable.qq, R.drawable.dz,
            R.drawable.xl, R.drawable.tm, R.drawable.uc, R.drawable.wx};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.market);

        //初始化ListView控件
        mListView = (ListView) findViewById(R.id.lv);
        //ListView展示数据需要适配器 创建一个Adapter的实例
        MyBaseAdapter mAdapter = new MyBaseAdapter();
        //设置Adapter
        mListView.setAdapter(mAdapter);
    }

    class MyBaseAdapter extends BaseAdapter {//BaseAdapter需要实现4个方法

        //返回item总数
        @Override
        public int getCount() {
            //返回ListView Item条目的总数
            return names.length;
        }

        //得到Item代表的对象
        @Override
        public Object getItem(int position) {
            //返回ListView Item条目代表的对象
            return names[position];
        }

        //得到Item的id
        @Override
        public long getItemId(int position) {
            //返回ListView Item的id
            return position;
        }

        // ListView优化 得到ItemView视图
        @Override // convertView  历史缓存对象
        public View getView(int position, View convertView, ViewGroup parent) {
            //将list_item布局转换成一个View对象 打气筒 View.inflate();
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).
                        inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder();
                holder.mTextView = (TextView) convertView.findViewById
                        (R.id.item_tv);
                holder.imageView = (ImageView) convertView.findViewById(R.id.item_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTextView.setText(names[position]);
            holder.imageView.setBackgroundResource(icons[position]);
            return convertView;
        }

        class ViewHolder {
            TextView mTextView;
            ImageView imageView;
        }

//        //得到ItemView视图
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            //将list_item布局转换成一个View对象 打气筒 View.inflate();
//            View view = View.inflate(MainActivity.this, R.layout.list_item, null);
//            //找到条目中的控件
//            ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
//            TextView mTextView = (TextView) view.findViewById(R.id.item_tv);
//            mTextView.setText(names[position]);
//            imageView.setBackgroundResource(icons[position]);
//            return view;
//        }

    }

}

Android ListView实战演练

运行截图 :
Android ListView实战演练

点个赞呗~

相关标签: 教学