Android BaseAdapter的使用与优化

释放双眼,带上耳机,听听看~!

MainActivity代码

package com.imooc.www.moocdemo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/5/11.
*/
public class BaseAdapterActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseadapteractivity);
//数据源
List<ItemListBean> itemListBeen = new ArrayList<>();
for(int i=0;i<21;i++){
itemListBeen.add(new ItemListBean(R.mipmap.ic_launcher,"我是标题"+i,"我是内容"+i));
}
ListView listView = (ListView) findViewById(R.id.baselistview);
listView.setAdapter(new MyBaseAdapter(this,itemListBeen));
}
}

适配器代码 此处有三种方式 逐步优化 第一种没有优化 第二种使用缓存 第三种 使用缓存并且减少findViewById的操作 提升性能

 

package com.imooc.www.moocdemo;
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 java.util.List;
/**
* Created by Administrator on 2016/5/11.
*/
public class MyBaseAdapter extends BaseAdapter {
private List<ItemListBean> mList;
private LayoutInflater mInflater;
public MyBaseAdapter(Context context, List<ItemListBean> list) {
mList = list;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//简单模式 没有优化
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//
//        View view = mInflater.inflate(R.layout.baselistitem,null);
//        ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
//        TextView title = (TextView) view.findViewById(R.id.iv_title);
//        TextView context = (TextView) view.findViewById(R.id.iv_content);
//
//        ItemListBean bean = mList.get(position);
//        imageView.setImageResource(bean.ItemImageResid);
//        title.setText(bean.ItemTitle);
//        context.setText(bean.ItemContent);
//
//        return view;
//    }
////普通模式 利用view 缓存 优化
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//    //判断如果此view没有缓存过则新建 入果有缓存直接利用缓存
//        if(convertView==null){
//            convertView = mInflater.inflate(R.layout.baselistitem,null);
//        }
//
//        View view = mInflater.inflate(R.layout.baselistitem,null);
//        ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
//        TextView title = (TextView) convertView.findViewById(R.id.iv_title);
//        TextView context = (TextView) convertView.findViewById(R.id.iv_content);
//
//        ItemListBean bean = mList.get(position);
//        imageView.setImageResource(bean.ItemImageResid);
//        title.setText(bean.ItemTitle);
//        context.setText(bean.ItemContent);
//
//        return convertView;
//    }
//优化模式 利用view 缓存 优化同时设置ViewHolder 减少findViewById进行优化
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//判断如果此view没有缓存过则新建 入果有缓存直接利用缓存
if (convertView == null) {
convertView = mInflater.inflate(R.layout.baselistitem, null);
viewHolder = new ViewHolder();
viewHolder.himageView = (ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.htitle = (TextView) convertView.findViewById(R.id.iv_title);
viewHolder.hcontenxt = (TextView) convertView.findViewById(R.id.iv_content);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ItemListBean bean = mList.get(position);
viewHolder.himageView.setImageResource(bean.ItemImageResid);
viewHolder.htitle.setText(bean.ItemTitle);
viewHolder.hcontenxt.setText(bean.ItemContent);
return convertView;
}
//优化模式使用
class ViewHolder {
public ImageView himageView;
public TextView htitle;
public TextView hcontenxt;
}
}

 

 

 

数据源ListBean代码

 

package com.imooc.www.moocdemo;
/**
* Created by Administrator on 2016/5/11.
*/
public class ItemListBean  {
public int ItemImageResid;
public String ItemTitle;
public String ItemContent;
//Alt+insert 快捷键生成构造函数
public ItemListBean(int itemImageResid, String itemTitle, String itemContent) {
ItemImageResid = itemImageResid;
ItemTitle = itemTitle;
ItemContent = itemContent;
}
}

布局文件代码 比较简单 重要的是 Item布局有两个属性 没用过 值得学习

 

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

item布局

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="60dp"
android:src="https://blog.csdn.net/csdndouniwan/article/details/51375621/@mipmap/ic_launcher"
android:layout_height="60dp" />
<TextView
android:id="@+id/iv_title"
android:layout_toEndOf="@id/iv_image"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="TITLE"
android:gravity="center"
android:textSize="25sp"
<strong><em><span> android:layout_toRightOf="@id/iv_image"</span></em></strong> />
<TextView
android:id="@+id/iv_content"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="我是内容"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_toRightOf="@id/iv_image"
android:layout_below="@id/iv_title" />
</RelativeLayout>

 

人已赞赏
Android文章

Android ListView 的简单应用包括点击事件

2020-3-26 23:33:03

Android文章

Java设计模式透析之适配器(Adapter)

2020-3-27 0:48:12

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索