用android studio 在android 7中运行的 

由UIActivityt 和 listview_layout.xml 和 list_item.xml写出来ListView用例

 

UIActivityt 代码

package com.example.a20200712;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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

public class UIActivity extends AppCompatActivity {

    private ListView listview = null;
    private List<Map<String,Object>> list = null;
    private BaseAdapter ba = null;
    /**
     * 原数据
     */
    public void init() {
        list = new ArrayList<Map<String,Object>>(3);

        for(int i = 1 ;i < 50;i++){
            Map<String,Object> map1 = new HashMap<String,Object>();
            map1.put("img" ,R.drawable.kf);
            map1.put("head","第"+i+"张");
            map1.put("txt","数字"+i+i+i+i+i);
            map1.put("id",Long.valueOf(i));
            list.add(map1);
        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);
        init();
        showListView();
    }


    public void showListView() {
        listview = findViewById(R.id.layout_listView);
        ba = new BaseAdapter() {
            //获取列表总条数
            @Override
            public int getCount() {
                return list.size();
            }

            //获取对应的list单个对象 在单击事件中返回对应的对象信息
            @Override
            public Object getItem(int position) {
                return list.get(position);
            }
            //表列对应数据的id 尽量不要直接使用 position 动态列表在某些场景下会出错
            @Override
            public long getItemId(int position) {
                return (Long) list.get(position).get("id");
            }

            //组装每一条list行 视图
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder = null;
                TextView id = null;
                ImageView iv = null;
                TextView head = null;
                TextView txt = null;

                if(convertView == null){
                    convertView = View.inflate(UIActivity.this,R.layout.list_item,null);
                    viewHolder = new ViewHolder();

                    id = convertView.findViewById(R.id.layout_list_item_id);
                    iv = convertView.findViewById(R.id.layout_list_item_img);
                    head = convertView.findViewById(R.id.layout_list_item_head);
                    txt = convertView.findViewById(R.id.layout_list_item_txt);
                    viewHolder.setIdV(id);
                    viewHolder.setImV(iv);
                    viewHolder.setHeadV(head);
                    viewHolder.setTxtV(txt);

                    //内容的点击事件
                    txt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("mw","文字-事件被触发了"+position);
                            Map<String,Object> currentMap = (Map<String,Object>) list.get(position);
                            list.remove(currentMap);
                            ba.notifyDataSetChanged();
                        }
                    });
                    //图处点击事件
                    iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("mw","图片-事件被触发了"+position);
                            Map<String,Object> currentMap = (Map<String,Object>) list.get(position);
                            currentMap.put("head","修改后的数据"+(String)currentMap.get("head"));
                            ba.notifyDataSetChanged();
                        }
                    });
                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder)convertView.getTag();
                     id = viewHolder.getIdV();
                     iv = viewHolder.getImV();
                     head = viewHolder.getHeadV();
                     txt = viewHolder.getTxtV();
                }

                id.setText(String.valueOf(list.get(position).get("id")));
                iv.setImageResource((Integer) list.get(position).get("img"));
                head.setText((String)list.get(position).get("head"));
                txt.setText((String)list.get(position).get("txt"));

                return convertView;
            }
        };
        listview.setAdapter(ba);


        //添加点击事件====对整行添加事件
      /*  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String,Object> currentMap = (Map<String,Object>) adapterView.getItemAtPosition(i);
               //Toast.makeText(getApplicationContext(), (String)currentMap.get("head"), Toast.LENGTH_SHORT).show();
                currentMap.put("head","修改后的数据"+(String)currentMap.get("head"));
                ba.notifyDataSetChanged();// 通知修改数据
            }
        });*/
    }

/*    public void delImgOnclick(View view){
       Log.i("mw","单个图片事件被触发了");
       Map map =  list.get(list.size()-1);
        list.remove(map);
       // Toast.makeText(getApplicationContext(),  String.valueOf(view.getId()), Toast.LENGTH_SHORT).show();
        ba.notifyDataSetChanged();// 通知修改数据
    }*/

    public class ViewHolder{
        private TextView idV;
        private ImageView imV;
        private TextView headV;
        private TextView txtV;

        public TextView getIdV() {
            return idV;
        }

        public void setIdV(TextView idV) {
            this.idV = idV;
        }

        public ImageView getImV() {
            return imV;
        }

        public void setImV(ImageView imV) {
            this.imV = imV;
        }

        public TextView getHeadV() {
            return headV;
        }

        public void setHeadV(TextView headV) {
            this.headV = headV;
        }

        public TextView getTxtV() {
            return txtV;
        }

        public void setTxtV(TextView txtV) {
            this.txtV = txtV;
        }
    }
}

 

listview_layout.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout_listView">
</ListView>

list_item.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">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:id="@+id/layout_list_item_id"></TextView>

    <ImageView
        android:id="@+id/layout_list_item_img"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        tools:srcCompat="@tools:sample/avatars"
        />

    <TextView
        android:id="@+id/layout_list_item_head"
        android:layout_width="283dp"
        android:layout_height="65dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="0dp"
        android:textSize="30dp"
        android:layout_toRightOf="@+id/layout_list_item_img"
        android:text="TextView" />

    <TextView
        android:id="@+id/layout_list_item_txt"
        android:layout_width="match_parent"
        android:textSize="30dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/layout_list_item_head"
        android:layout_toRightOf="@+id/layout_list_item_img"
        android:layout_height="65dp"
        android:text="TextView" />


</RelativeLayout>

在 android 7上运行

原文链接:https://blog.csdn.net/m0_37622302/article/details/107311135

最后修改日期:2020年7月15日