• android开发 一个更优的listView的写法


    布局xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ListLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#FFF"
        android:padding="10dp">
        <ImageView
            android:id="@+id/myImageView"
            android:src="@drawable/bblood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp">
            <TextView
                android:id="@+id/TextName"
                android:text="这里是名称显示"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:textColor="#000"/>
            <TextView
                android:id="@+id/Description"
                android:text="这里是描述显示"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:textColor="#000"/>
    
        </LinearLayout>
    </LinearLayout>

    list内容适配器(使用了子布局缓存和主布局复用):

    package com.example.lenovo.myapplication;
    
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.List;
    
    /**
     * Created by lenovo on 2018/4/25.
     */
    
    public class ListAdapter extends ArrayAdapter<ListArray> {
        private int ListLayoutId;
        public ListAdapter(Context context , int textViewResourrceId, List<ListArray> obj){
            super(context,textViewResourrceId,obj);
            this.ListLayoutId = textViewResourrceId;
    
        }
    
    
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ListArray listArray = getItem(position);
            View view ;
            ViewHolder viewHolder;
            if(convertView == null){
                view = LayoutInflater.from(getContext()).inflate(ListLayoutId,parent,false);
                viewHolder = new ViewHolder();
                viewHolder.HmyImageView = (ImageView)view.findViewById(R.id.myImageView);
                viewHolder.HtextName = (TextView)view.findViewById(R.id.TextName);
                viewHolder.Hdescription = (TextView)view.findViewById(R.id.Description);
                view.setTag(viewHolder);
            }else {
                view = convertView;
                viewHolder = (ViewHolder)view.getTag();
            }
            viewHolder.HmyImageView.setImageResource(listArray.getIconId());
            viewHolder.HtextName.setText(listArray.getName());
            viewHolder.Hdescription.setText(listArray.getDescription());
            return view;
        }
        class  ViewHolder{
            ImageView HmyImageView;
            TextView HtextName;
            TextView Hdescription;
        }
    }



    listView实现activity类:

        

    package com.example.lenovo.myapplication;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListDemoActivity extends AppCompatActivity {
        private List<ListArray> listArray = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_demo);
            dataImport();
            ListAdapter listAdapter = new ListAdapter(ListDemoActivity.this,R.layout.list_layout,listArray);
            ListView listView = (ListView)findViewById(R.id.ListView01);
            listView.setAdapter(listAdapter);
        }
        private void dataImport(){
            ListArray listArray1 = new ListArray("Blood","血",R.drawable.bblood);
            listArray.add(listArray1);
            ListArray listArray2 = new ListArray("BloodRing","血环",R.drawable.bbloodrring);
            listArray.add(listArray2);
            ListArray listArray3 = new ListArray("ColoredFlowers","五色花",R.drawable.ccoloredfflowers);
            listArray.add(listArray3);
            ListArray listArray4 = new ListArray("Delight","欢欣",R.drawable.ddelight);
            listArray.add(listArray4);
            ListArray listArray5 = new ListArray("DrippingFlower","水滴花",R.drawable.ddrippingfflower);
            listArray.add(listArray5);
            ListArray listArray6 = new ListArray("EyeOfFlame","火焰之眼",R.drawable.eeyeooffflame);
            listArray.add(listArray6);
            ListArray listArray7 = new ListArray("EyeOfRotation","旋转之眼",R.drawable.eeyeoofrrotation);
            listArray.add(listArray7);
            ListArray listArray8 = new ListArray("Flame","火焰",R.drawable.fflame);
            listArray.add(listArray8);
            ListArray listArray9 = new ListArray("FourPetals","四叶花",R.drawable.ffourppetals);
            listArray.add(listArray9);
            ListArray listArray10 = new ListArray("Gradient","渐变",R.drawable.ggradient);
            listArray.add(listArray10);
            ListArray listArray11 = new ListArray("GreenLeaf","绿叶",R.drawable.ggreenlleaf);
            listArray.add(listArray11);
            ListArray listArray12 = new ListArray("IronAngle","铁角花",R.drawable.iironaangle);
            listArray.add(listArray12);
            ListArray listArray13 = new ListArray("IronThorns","绿叶",R.drawable.iirontthorns);
            listArray.add(listArray13);
            ListArray listArray14 = new ListArray("lookUp","向上看",R.drawable.llookuup);
            listArray.add(listArray14);
            ListArray listArray15 = new ListArray("Rotatingleaves","旋叶",R.drawable.rrotatingleaves);
            listArray.add(listArray15);
            ListArray listArray16 = new ListArray("ShadowFlow","暗影流",R.drawable.sshadowfflow);
            listArray.add(listArray16);
            ListArray listArray17 = new ListArray("TechnologyEye","科技眼",R.drawable.tthreelleaves);
            listArray.add(listArray17);
            ListArray listArray18 = new ListArray("ThreeLeaves","三叶",R.drawable.iirontthorns);
            listArray.add(listArray18);
    
    
        }
    
    }
    

  • 相关阅读:
    VirtualBox 命令行操作
    [大数据入门]实战练习 安装Cloudera-Hadoop集群
    DB2 Package Issues and Solution
    关于《阿里巴巴Java开发规约》插件的安装与使用
    Spring学习笔记:Spring概述,第一个IoC依赖注入案例
    SpringMVC:系统认识一下maven
    利用JS提交表单的几种方法和验证(必看篇)
    a标签调用js的几种方法
    使用BigDecimal进行精确运算
    Maven中settings.xml的配置项说明
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708633.html
Copyright © 2020-2023  润新知