Java教程

java android studio 安卓开发 - ListView

本文主要是介绍java android studio 安卓开发 - ListView,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


=========================
MainActivity:
package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity{
    public ListView listView;
//        定义需要的适配器数据
//        textview名称
    public String[] names = new String[]{"Dave", "SteelBlue", "LightSkyBlue", "SkyBlue", "DeepSkyBlue", "LightBLue", "PowDerBlue", "CadetBlue", "Azure", "LightCyan", "PaleTurquoise", "Cyan", "Aqua", "DarkTurquoise", "DarkSlateGray", "DarkCyan", "Teal", "MediumTurquoise", "LightSeaGreen", "Turquoise", "Auqamarin"};
    //        图片数据
    public Integer[] icons = new Integer[]{R.drawable.pic_0, R.drawable.pic_1, R.drawable.pic_2,
            R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6,
            R.drawable.pic_7, R.drawable.pic_8, R.drawable.pic_9, R.drawable.pic_10,
            R.drawable.pic_11, R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14,
            R.drawable.pic_15, R.drawable.pic_16, R.drawable.pic_17, R.drawable.pic_18,
            R.drawable.pic_19, R.drawable.pic_20};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.lv);
//        listView 展示数据需要适配器
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter();
//        设置adapter
        listView.setAdapter(myBaseAdapter);

    }
    class MyBaseAdapter extends BaseAdapter{
//        返回item总数
        @Override
        public int getCount() {
            return icons.length;
        }

        @Override
        public Object getItem(int position) {
            return icons[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
//        得到item的View视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            把list_item文件转化为View对象返回
//            View view = View.inflate(MainActivity.this, R.layout.list_item, null);
//            ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
//            TextView textView = (TextView) view.findViewById(R.id.item_text);
//            imageView.setImageResource(icons[position]);
//            textView.setText(names[position]);

//            优化 convertView复用
            ViewHolder viewHolder;
            if (convertView == null){
//            把list_item文件转化为View对象
                convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.item_image);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.item_text);
                convertView.setTag(viewHolder);
            }else{
//            如果convertView不为空
                viewHolder = (ViewHolder)convertView.getTag();
            }
            viewHolder.textView.setText(names[position]);
            viewHolder.imageView.setImageResource(icons[position]);

            return convertView;
        }
    }
    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }

}
=========================
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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:divider="#4682B4"
        android:dividerHeight="1dp"
        android:layout_height="match_parent">

    </ListView>
</RelativeLayout>
=========================
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="300dp"
        android:layout_height="180dp"

        android:layout_margin="8dp"/>
    <TextView
        android:id="@+id/item_text"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你过来啊"
        android:textStyle="bold"
        android:textSize="20dp"/>

</LinearLayout>

这篇关于java android studio 安卓开发 - ListView的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!