其他分享
首页 > 其他分享> > ListView(优化),自定义Layout和adapter,entity,ViewHolder,ConvertView用法案例(phone)

ListView(优化),自定义Layout和adapter,entity,ViewHolder,ConvertView用法案例(phone)

作者:互联网

自定义实体类:PhoneEntity.java
package com.example.entity;

/**
 * Created by Administrator on 2017/2/16.
 */

public class PhoneEntity {
    private int img;
    private String name,price,countNum;

    public int getImg() {
        return img;
    }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }

    public String getCountNum() {
        return countNum;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setCountNum(String countNum) {
        this.countNum = countNum;
    }
}
自定义布局(Layout) acvitity_phone_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp">
    <ImageView
        android:src="@drawable/ihone7plus"
        android:id="@+id/img_phone"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/phone_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ihone7Plus玫瑰金"
            android:textSize="20dp"
            android:layout_marginTop="50sp"/>
        <TextView
            android:id="@+id/phone_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="¥7188"
            android:textSize="20dp"
            android:textColor="#f45959" />
        <TextView
            android:id="@+id/phone_counter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="50353条评价"
            android:textSize="15dp"
            android:textColor="@android:color/darker_gray"/>

    </LinearLayout>


</LinearLayout>
自定义acvitity_phone.xml(定义ListView)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_phone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jump.PhoneActivity">
    <ListView
        android:id="@+id/lv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>

</RelativeLayout>
自定义PhoneAcvitity
package com.example.jump;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.adapter.PhoneAdapter;
import com.example.entity.PhoneEntity;

import java.util.ArrayList;
import java.util.List;

public class PhoneActivity extends AppCompatActivity {
    private ListView lv;
    private int[] ids=new int[]{R.drawable.ihone7plus,R.drawable.mi5s,R.drawable.leshi2,R.drawable.rongyao8};
     private String[] phonenames=new String[]{"苹果7Plus,128G玫瑰金","小米5S全网通版,32G亚光金","乐视2原力金32GB内存","荣耀8全网通4GB32G珠光白"};
    private String[] phoneprice=new String[]{"¥7188","¥1899","¥1099","¥2229"};
    private String[] phonecounter=new String[]{"50353条评价","20353条评价","34553条评价","65053条评价"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        lv= (ListView) findViewById(R.id.lv_phone);
        intiCustomPhoneAdapter();
    }
    private void intiCustomPhoneAdapter(){
        List<PhoneEntity> list =new ArrayList<PhoneEntity>();
        for (int i = 0; i < phonenames.length; i++) {
            PhoneEntity phone=new PhoneEntity();
            phone.setImg(ids[i]);
            phone.setName(phonenames[i]);
            phone.setPrice(phoneprice[i]);
            phone.setCountNum(phonecounter[i]);
            list.add(phone);
        }
        PhoneAdapter phoneada=new PhoneAdapter(list,this);
        lv.setAdapter(phoneada);
    }
}
自定义adapter(PhoneAdapter)
复用convertview,减少item的数量:节省内存,运行更流畅
使用ViewHolder类,减少findviewbyid的次数,好处就是运行更流畅
package com.example.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.entity.PhoneEntity;
import com.example.jump.R;

import java.util.List;

/**
 * Created by Administrator on 2017/2/16.
 */

public class PhoneAdapter extends BaseAdapter {
    private List<PhoneEntity> list;//数据源
    private Context context;//上下文对象
    private LayoutInflater inflater;

    public PhoneAdapter(List<PhoneEntity> list, Context context) {
        this.list = list;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }
    //有参的构造函数,为数据源,上下文对象复制,同时实例化布局加载器
    //有多少条数据,需要创建多少个item布局
    @Override
    public int getCount() {
        return list.size();
    }
    //返回position对应位置的数据
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    //返回position对应位置item的id
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 具体定义加载item布局,并将数据显示到item布局上的方法。
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        //加载item布局 将xml布局加载到内存中,形成一个view
        if(convertView==null){ //被复用后的convertview类依然包含tag
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.activity_phone_list,null);
            holder.iv= (ImageView) convertView.findViewById(R.id.img_phone);
            holder.tv_phonename= (TextView) convertView.findViewById(R.id.phone_name);
            holder.tv_phoneprice= (TextView) convertView.findViewById(R.id.phone_price);
            holder.tv_phonecounter= (TextView) convertView.findViewById(R.id.phone_counter);
            convertView.setTag(holder);
        }else {//convertview不为空,不为空则说明convertview包含tag
           holder= (ViewHolder) convertView.getTag();
        }
//        View view=inflater.inflate(R.layout.activity_phone_list,null);
        //实例化item布局上的控件
//          ImageView iv= (ImageView) view.findViewById(R.id.img_phone);
//          TextView tv_phonename= (TextView) view.findViewById(R.id.phone_name);
//          TextView tv_phoneprice= (TextView) view.findViewById(R.id.phone_price);
//          TextView tv_phonecounter= (TextView) view.findViewById(R.id.phone_counter);
        //往控件上显示数据
        //获取position对应位置的数据
        PhoneEntity phonety= (PhoneEntity) getItem(position);
        holder.iv.setImageResource(phonety.getImg());
        holder.tv_phonename.setText(phonety.getName());
        holder.tv_phoneprice.setText(phonety.getPrice());
        holder.tv_phonecounter.setText(phonety.getCountNum());
        return convertView;
    }
    class ViewHolder{
        public ImageView iv;
        public  TextView tv_phonename;
        public  TextView tv_phoneprice;
        public TextView tv_phonecounter;
    }
}
MainAcvitity
package com.example.jump;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.adapter.MyAdapter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button textButton, tv_btn, btn_edit, btn_cb, btn_imgsca, work_activity, rg_btn, timedatepick_btn, arrayadapter, btn_myadapter, btn_phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        tv_btn = (Button) findViewById(R.id.btn_test);
        tv_btn.setOnClickListener(this);
        btn_edit = (Button) findViewById(R.id.btn_edit);
        btn_edit.setOnClickListener(this);
        btn_cb = (Button) findViewById(R.id.btn_cb);
        btn_cb.setOnClickListener(this);
        btn_imgsca = (Button) findViewById(R.id.btn_imgsca);
        btn_imgsca.setOnClickListener(this);
        work_activity = (Button) findViewById(R.id.work_activity);
        work_activity.setOnClickListener(this);
        rg_btn = (Button) findViewById(R.id.rg_btn);
        rg_btn.setOnClickListener(this);
        timedatepick_btn = (Button) findViewById(R.id.datetimepick_btn);
        timedatepick_btn.setOnClickListener(this);
        arrayadapter = (Button) findViewById(R.id.lv_arrayadapter);
        arrayadapter.setOnClickListener(this);
        btn_myadapter = (Button) findViewById(R.id.btn_myadapter);
        btn_myadapter.setOnClickListener(this);
        btn_phone = (Button) findViewById(R.id.btn_phone);
        btn_phone.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_edit:
                startNewApplication(EditTestActivity.class);
                break;
            case R.id.btn_cb:
                startNewApplication(CheckBoxActivity.class);
            case R.id.btn_imgsca:
                startNewApplication(ImageScaleActivity.class);
            case R.id.work_activity:
                startNewApplication(WorkActivity.class);
                break;
            case R.id.rg_btn:
                startNewApplication(RadioGroupActivity.class);
                break;
            case R.id.datetimepick_btn:
                startNewApplication(TimeDatePickerActivity.class);
                break;
            case R.id.btn_myadapter:
                startNewApplication(ArrayAdapterActivity.class);
                break;
            case R.id.btn_phone:
                startNewApplication(PhoneActivity.class);
                break;
        }

    }

    private void startNewApplication(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
}

标签:findViewById,ViewHolder,Layout,自定义,public,phone,import,btn,id
来源: https://blog.51cto.com/u_15249199/2847153