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

布局xml



    
    
        
        

    

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 {
    private int ListLayoutId;
    public ListAdapter(Context context , int textViewResourrceId, List 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 = 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);


    }

}

你可能感兴趣的:(android开发)