Android中ListView以及简易适配器(simpleAdapter)的使用

MainActivity.java

package com.example.first.myapplication;

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

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

public class MainActivity extends Activity {

    ListView listView;
    SimpleAdapter simpleAdapter;
    List<Map<String,Object>>datalist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = this.findViewById(R.id.listview1);
        /*
            第一个参数 上下文
            第二个参数 数据源 list集合
            第三个参数 列表项布局文件的id
            第四个参数 布局文件控件id对应的键名;
            第五个参数 布局文件控件的id
         */
        datalist = new ArrayList<Map<String,Object>>();
        simpleAdapter = new SimpleAdapter(MainActivity.this , getDatalist() , R.layout.item , new String[]{"img1","txtv1"} ,
                new int[]{R.id.img1,R.id.txtv1});
       /*
            视图加载适配器
        */
        listView.setAdapter(simpleAdapter);

        /*
            事件监听器
         */
        //点击列表项中单个条目的监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            //position 当前点击的位置,也就是当前点击的条目,从0开始
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                //当前点击的条目的内容
                String text = listView.getItemAtPosition(position)+"";
                Toast.makeText(MainActivity.this,"position="+position+"text="+text,Toast.LENGTH_SHORT).show();
            }
        });

        //滚动变化时的监听器
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                /*
                    i:滚动状态;
                 */
                switch (i){
                    case SCROLL_STATE_FLING:
                        Toast.makeText(MainActivity.this,"用户手指在离开屏幕之前,由于用力滑了一下,视图仍依靠惯性继续滚动",Toast.LENGTH_SHORT).show();
                        Map<String,Object>map = new HashMap<String,Object>();
                        map.put("img1",R.mipmap.ic_launcher);
                        map.put("txtv1","增加项");
                        datalist.add(map);
                        simpleAdapter.notifyDataSetChanged();
                        break;
                    case SCROLL_STATE_IDLE:
                        Toast.makeText(MainActivity.this,"视图已经停止滑动",Toast.LENGTH_SHORT).show();
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL:
                        Toast.makeText(MainActivity.this,"用户手指没有离开屏幕,正在滑动",Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
    }

    private List<Map<String,Object>> getDatalist(){
        for(int i = 0;i<=25;i++){
            Map<String,Object>map = new HashMap<String,Object>();
            map.put("img1",R.mipmap.ic_launcher);
            map.put("txtv1","今天是"+i+"号");
            datalist.add(map);
        }
        return datalist;
    }
}

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:orientation="horizontal">
        <ImageView
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/txtv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#000000"
            android:text="demo"/>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.first.myapplication.MainActivity">

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

</android.support.constraint.ConstraintLayout>

猜你喜欢

转载自blog.csdn.net/tdl081071tdy/article/details/83307477