24.Android中的列表--ListView

ListView

1.简单列表--ArrayAdapter

<?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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:orientation="vertical"
        android:paddingLeft="10dp">
        <Button
            android:id="@+id/btn_array_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Array列表"/>
        <Button
            android:id="@+id/btn_array_simple"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Simple列表"/>
    </LinearLayout>

</ScrollView>

<?xml version="1.0" encoding="utf-8"?>
<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=".ArrayListActivity">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_array_list = findViewById(R.id.btn_array_list);
        btn_array_list.setOnClickListener(this);

        Button btn_array_simple = findViewById(R.id.btn_array_simple);
        btn_array_simple.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_array_list){
            Intent intent = new Intent(this, ArrayListActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_array_simple) {
            Intent intent = new Intent(this, SimpleListActivity.class);
            startActivity(intent);
        }
    }
}

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

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

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

public class ArrayListActivity extends AppCompatActivity {
    private ListView mListView;
    private List<String> mStringList;
    private ArrayAdapter<String> mArrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_array_list);

        mListView = findViewById(R.id.lv);

        mStringList = new ArrayList<>();

        for (int i = 0; i < 50; i++) {
            mStringList.add("这是条目"+i);
        }

        mArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mStringList);

        mListView.setAdapter(mArrayAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(ArrayListActivity.this,"你点击了"+i,Toast.LENGTH_LONG).show();
            }
        });
    }
}

2.图文列表--SimpleAdapter

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".SimpleListActivity">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

<?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"
    android:paddingLeft="10dp"
    android:paddingTop="5dp"
    android:paddingRight="10dp"
    android:paddingBottom="5dp">
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher_background"/>
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/iv_img"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="雨中漫步"/>
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/iv_img"
        android:ellipsize="end"
        android:maxLines="3"
        android:text="人生就像时一场旅行,不必在乎目的地,在乎你妈说的你妈的嘛嘛嘛才是能真的嘛嘛嘛对咯收到反馈大姐夫,啊塞德里克复健科老师的会计法 阿是两地分居"
        android:textSize="16sp"/>

</RelativeLayout>

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleListActivity extends AppCompatActivity {
    private ListView mListView;
    private SimpleAdapter mSimpleAdapter;
    private List<Map<String,Object>> mList;
    private int[] imgs = {
            R.drawable.test1,
            R.drawable.test2,
            R.drawable.test3,
            R.drawable.test4,
            R.drawable.test5,
            R.drawable.test6,
            R.drawable.test7,
            R.drawable.test8,
            R.drawable.test9,
            R.drawable.test10
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_list);

        mListView = findViewById(R.id.lv);
        mList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("img",imgs[i%imgs.length]);
            map.put("title","这是标题"+i);
            map.put("content","这是内容"+i);

            mList.add(map);
        }
        mSimpleAdapter = new SimpleAdapter(this,mList,
                R.layout.list_item_layout,
                new String[]{"img","title","content"},
                new int[]{R.id.iv_img,R.id.tv_title,R.id.tv_content});
        mListView.setAdapter(mSimpleAdapter);
    }
}

3.图文复杂列表--BaseAdapter

<?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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:orientation="vertical"
        android:paddingLeft="10dp">
        <Button
            android:id="@+id/btn_array_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Array列表"/>
        <Button
            android:id="@+id/btn_array_simple"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Simple列表"/>
        <Button
            android:id="@+id/btn_array_base"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="BaseAdapter列表"/>
    </LinearLayout>

</ScrollView>

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_array_list = findViewById(R.id.btn_array_list);
        btn_array_list.setOnClickListener(this);

        Button btn_array_simple = findViewById(R.id.btn_array_simple);
        btn_array_simple.setOnClickListener(this);

        Button btn_array_base = findViewById(R.id.btn_array_base);
        btn_array_base.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_array_list){
            Intent intent = new Intent(this, ArrayListActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_array_simple) {
            Intent intent = new Intent(this, SimpleListActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_array_base) {
            Intent intent = new Intent(this, BaseAdapterActivity.class);
            startActivity(intent);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".BaseAdapterActivity"
    android:orientation="vertical">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.listviewtest.adapter.MyAdapter;
import com.example.listviewtest.bean.ItemBean;

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

public class BaseAdapterActivity extends AppCompatActivity {
    private ListView mListView;
    private List<ItemBean> mBeanList;
    private MyAdapter mMyAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);

        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mMyAdapter = new MyAdapter(this,mBeanList);
        mListView.setAdapter(mMyAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemBean itemBean = mBeanList.get(i);
                String title = itemBean.getTitle();
                Toast.makeText(BaseAdapterActivity.this,"您点击了"+i+title,Toast.LENGTH_LONG).show();
            }
        });
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                return false;
            }
        });
    }

    private void initData() {
        mBeanList = new ArrayList<>();

        ItemBean itemBean1 = new ItemBean();
        itemBean1.setTitle("我的小黑狗");
        itemBean1.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
        itemBean1.setImgResId(R.drawable.test1);

        ItemBean itemBean2 = new ItemBean();
        itemBean2.setTitle("我的小白狗");
        itemBean2.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
        itemBean2.setImgResId(R.drawable.test2);

        ItemBean itemBean3 = new ItemBean();
        itemBean3.setTitle("我的小黑狗");
        itemBean3.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
        itemBean3.setImgResId(R.drawable.test3);

        ItemBean itemBean4 = new ItemBean();
        itemBean4.setTitle("我的小白狗");
        itemBean4.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
        itemBean4.setImgResId(R.drawable.test4);

        ItemBean itemBean5 = new ItemBean();
        itemBean5.setTitle("我的小黑狗");
        itemBean5.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
        itemBean5.setImgResId(R.drawable.test5);

        ItemBean itemBean6 = new ItemBean();
        itemBean6.setTitle("我的小白狗");
        itemBean6.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
        itemBean6.setImgResId(R.drawable.test6);

        ItemBean itemBean7 = new ItemBean();
        itemBean7.setTitle("我的小黑狗");
        itemBean7.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
        itemBean7.setImgResId(R.drawable.test7);

        ItemBean itemBean8 = new ItemBean();
        itemBean8.setTitle("我的小白狗");
        itemBean8.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
        itemBean8.setImgResId(R.drawable.test8);

        ItemBean itemBean9 = new ItemBean();
        itemBean9.setTitle("我的小黑狗");
        itemBean9.setContent("我的小黑狗,明天就到了,到底是死是活,没人知道,希望你不会挂");
        itemBean9.setImgResId(R.drawable.test9);

        ItemBean itemBean10 = new ItemBean();
        itemBean10.setTitle("我的小白狗");
        itemBean10.setContent("我的小白狗,以前跑丢了,希望你不会被狗贩子抓去杀了,卖狗肉");
        itemBean10.setImgResId(R.drawable.test10);

        mBeanList.add(itemBean1);
        mBeanList.add(itemBean2);
        mBeanList.add(itemBean3);
        mBeanList.add(itemBean4);
        mBeanList.add(itemBean5);
        mBeanList.add(itemBean6);
        mBeanList.add(itemBean7);
        mBeanList.add(itemBean8);
        mBeanList.add(itemBean9);
        mBeanList.add(itemBean10);

    }

    private void initView() {
        mListView = findViewById(R.id.lv);
    }
}

package com.example.listviewtest.adapter;

import android.content.Context;
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 com.example.listviewtest.R;
import com.example.listviewtest.bean.ItemBean;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<ItemBean> mBeanList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public MyAdapter(Context context, List<ItemBean> beanList){
        this.mContext = context;
        this.mBeanList = beanList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public int getCount() {
        return mBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return mBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = mLayoutInflater.inflate(R.layout.list_item_layout,viewGroup,false);

        ImageView imageView = view.findViewById(R.id.iv_img);
        TextView tvTitle= view.findViewById(R.id.tv_title);
        TextView tvContent = view.findViewById(R.id.tv_content);

        ItemBean itemBean = mBeanList.get(i);

        imageView.setImageResource(itemBean.getImgResId());
        tvTitle.setText(itemBean.getTitle());
        tvContent.setText(itemBean.getContent());

        return view;

    }
}

package com.example.listviewtest.bean;

public class ItemBean {
    private String title;
    private String content;
    private int imgResId;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getImgResId() {
        return imgResId;
    }

    public void setImgResId(int imgResId) {
        this.imgResId = imgResId;
    }

    @Override
    public String toString() {
        return "itemBean{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", imgResId=" + imgResId +
                '}';
    }
}

相关推荐

  1. android开发-12】androidListView详细用法介绍

    2024-02-01 06:38:01       28 阅读
  2. 华纳云:androidlistview数据动态加载方法

    2024-02-01 06:38:01       35 阅读
  3. flutterListView单元测试

    2024-02-01 06:38:01       13 阅读
  4. Android studio 之 ListView

    2024-02-01 06:38:01       43 阅读
  5. Android开发——ListView

    2024-02-01 06:38:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 06:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 06:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 06:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 06:38:01       20 阅读

热门阅读

  1. Golang k8s相关yaml包的区别

    2024-02-01 06:38:01       34 阅读
  2. Spring Boot接收xml参数

    2024-02-01 06:38:01       38 阅读
  3. 【C++】三角形(triangle)

    2024-02-01 06:38:01       23 阅读
  4. Uni-app 如何上传文件, 使用的API是什么

    2024-02-01 06:38:01       39 阅读
  5. 华为网络设备:核心命令一览

    2024-02-01 06:38:01       34 阅读
  6. 设计模式全览:编程艺术的精髓!

    2024-02-01 06:38:01       25 阅读
  7. 设计模式——策略模式

    2024-02-01 06:38:01       29 阅读
  8. Spring Boot + Vue3 实现七牛云大视频上传

    2024-02-01 06:38:01       32 阅读
  9. 3D人体运动重建

    2024-02-01 06:38:01       41 阅读