ListView用例配合ViewHolder缓存

   日期:2020-07-16     浏览:93    评论:0    
核心提示:用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.view.View;import androi...

用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);


        //添加点击事件====对整行添加事件
      
    }



    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上运行

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服