ListView的使用-模拟微博随便看看栏目【执行与优化】

时间:2024-01-07 21:44:50

今天我们来讲述一下如何使用ListView来模仿微博随便看看栏目(ps:这是老师布置的作业,所以…),在前篇博客中,我们讲述了细解ListView之自定义适配器的使用,所以本篇我们不以特别详细的讲述(ps:细解ListView之自定义适配器:http://www.cnblogs.com/boy1025/p/4438999.html),本程序的链接附在文章的正下方,欢迎下载【版权所有,转载请注明出处,谢谢】

下面我们进入正题,首先我们来看下效果图:

ListView的使用-模拟微博随便看看栏目【执行与优化】

--------------------------------------------------------------------------------------------------------------------------

【分析】

要完成这个小功能,我们需要:

1.ListView及ListView_Item

2.实体类的编写

3.自定义适配器的书写

4.ListView的绑定数据源与控件

----->>在上述分析中,最重要的是自定义适配器的书写,此功能我们继承是BaseAdapter

【编码】

1.首先我们来看自定义适配器MyAdapter:

package cn.edu.bzu.adapter;

import java.util.List;

import cn.edu.bzu.entity.Information;
import cn.edu.bzu.weibo_looking.R;
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; /**
* 自定义Adapter实现listView的显示
* @author monster
* @date : 2015-04-28
*/
public class MyAdapter extends BaseAdapter {
private List<Information> list;
private Context mContext;
private LayoutInflater mInflater;
//定义构造方法
public MyAdapter(List<Information> list, Context mContext) {
super();
this.list = list;
this.mContext = mContext;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=mInflater.from(mContext).inflate(R.layout.listview_item,null); //布局转换成视图
holder=new ViewHolder();
//实例化控件
holder.Image=(ImageView)convertView.findViewById(R.id.Iv);
holder.Name=(TextView)convertView.findViewById(R.id.Name);
holder.Time=(TextView)convertView.findViewById(R.id.Time);
holder.Contents=(TextView)convertView.findViewById(R.id.Contents);
convertView.setTag(holder); //设置setTag使得程序得以优化
}else{
holder=(ViewHolder) convertView.getTag();
}
//给控件赋值
holder.Image.setBackgroundResource(list.get(position).getImgId());
holder.Name.setText(list.get(position).getName());
holder.Time.setText(list.get(position).getDate());
holder.Contents.setText(list.get(position).getContent());
return convertView;
} class ViewHolder{
ImageView Image;
TextView Name;
TextView Time;
TextView Contents;
}
}

PS:在继承BaseAdapter 需要实现getCount,getItem,getItemId,getView,以及构造方法 等方法,里面最重要的是getView方法,本文用到ViewHolder设计模式,具体的ViewHolder设计模式的优点,请参考(http://www.cnblogs.com/boy1025/p/4438999.html

2.MainActivity的编写

package cn.edu.bzu.weibo_looking;

import java.util.ArrayList;
import java.util.List; import cn.edu.bzu.adapter.MyAdapter;
import cn.edu.bzu.entity.Information;
import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity; public class MainActivity extends Activity {
private ListView listView;
private MyAdapter adapter;
private List<Information> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();//数据源
initView();
}
private void initView() {
// 初始化控件
listView=(ListView) findViewById(R.id.Lv);
listView.setAdapter(adapter); //绑定适配器
}
private void setData() {
// 设置数据源
list=new ArrayList<Information>();
list.add(new Information(R.drawable.img1,"monsterLin","2015-04-28","Do whatever you want"));
list.add(new Information(R.drawable.img2, "HuZi", "2015-04-28", "老板给我涨工资了,好开森"));
list.add(new Information(R.drawable.img3,"YY","2015-04-28","他变了,他真的变了,他真的真的变帅了"));
list.add(new Information(R.drawable.img4,"XiaoY","2015-04-28","Lalala德玛西亚"));
list.add(new Information(R.drawable.img5,"BigWhite","2015-04-28","妹子,你好,我是雷锋"));
list.add(new Information(R.drawable.img6, "DongDong", "2015-04-28", "脱单了,我好开心啊,啦啦啦"));
list.add(new Information(R.drawable.img7, "XiaoXin", "2015-04-28", "今天和闺蜜去超市,买了好多好多好吃的##"));
list.add(new Information(R.drawable.img8, "Robi", "2015-04-28", "这个Vpn,我推荐Xroute,天然无公害~"));
adapter=new MyAdapter(list, MainActivity.this);
}
}

Ps:在书写MainActivity的时候:

1.建立数据源

2.实例化控件

3.绑定适配器

------------------------------------------------------------------------------------------------------------------------

【优化篇】

优化思路:由于每次书写自定义适配器的时候总是会书写构造方法,getCount(),getItem(),getItemId,getView()方法,以及ViewHolder方法 所以我们对其进行优化,优化是书写CommonAdapter,ViewHolder这两个工具类,在书写适配器的时候继承CommonAdapter即可

---->>代码部分:

CommonAdapter.java:

package cn.edu.bzu.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; import java.util.List; import cn.edu.bzu.weibo_looking.R; /**
* CommonAdapter makes Common extends BaseAdapter
* Created by monster on 2015/5/3.
*/
public abstract class CommonAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
protected LayoutInflater mInflater; public CommonAdapter(Context context,List<T>datas){
this.mContext=context;
this.mDatas=datas;
mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return mDatas.size();
} @Override
public T getItem(int position) {
return mDatas.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder=ViewHolder.get(mContext,convertView,parent, R.layout.listview_item,position);
convert(holder,getItem(position));
return holder.getConvertView();
}
public abstract void convert(ViewHolder holder,T t); }

ViewHolder.java:

package cn.edu.bzu.util;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; /**
* Created by monster on 2015/4/20.
* Have a good package ViewHolder
* get(Context context,View convertView,ViewGroup parent,int layoutId,int position) To make ViewHolder
*
*/
public class ViewHolder {
private SparseArray<View> mViews; //稀疏数组,SparseArray比HashMap执行效率高
private int mPosition;
private View mConvertView; //作用是方便系统在重写UI时,能重用原来实用过的View实例,以此来降低系统资源的消耗和提高代码效率
/*
*ConvertView的几点特征。
*对于一个ListView,Android保存所有曾经生成过的ConvertView实例,直至系统垃圾回收这些实例位置,而不是只保存最后使用的ConvertView对象。
*这些保存的ConvertView以使用时间顺序排序,并依次被传递到getView函数中。
*/
public ViewHolder(Context context,ViewGroup parent,int layoutId,int position){
//ViewHolder的构造方法
this.mPosition=position;
this.mViews=new SparseArray<View>(); //初始化SparseArray对象
mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
mConvertView.setTag(this);
} public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){
if(convertView==null){
return new ViewHolder(context,parent,layoutId,position);
}else{
ViewHolder holder=(ViewHolder)convertView.getTag();
holder.mPosition=position;
return holder;
}
} /**
* 通过viewId获取控件
* @param viewId
* @param <T>
* @return
*/
public <T extends View> T getView(int viewId){
View view=mViews.get(viewId);
if(view==null){
view=mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T)view;
}
public View getConvertView() {
return mConvertView;
} /**
* 为设置TextView的值
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId,String text){
TextView tv=getView(viewId);
tv.setText(text);
return this;
} /**
* 设置图片
* @param viewId
* @param resId
* @return
*/
public ViewHolder setImageResource(int viewId,int resId){
ImageView view=getView(viewId);
view.setImageResource(resId);
return this;
}
}

OptimizeAdapter.java

package cn.edu.bzu.adapter;

import java.util.List;

import android.content.Context;
import cn.edu.bzu.entity.Information;
import cn.edu.bzu.util.CommonAdapter;
import cn.edu.bzu.util.ViewHolder;
import cn.edu.bzu.weibo_looking.R; public class OptimizeAdapter extends CommonAdapter<Information> { public OptimizeAdapter(Context context, List<Information> datas) {
super(context, datas);
} @Override
public void convert(ViewHolder holder, Information information) {
//给控件赋值,通过getter/setter方法实现
holder.setText(R.id.Name,information.getName());
holder.setText(R.id.Time, information.getDate());
holder.setText(R.id.Contents,information.getContent());
holder.setImageResource(R.id.Iv, information.getImgId());
} }

--->优化总结:

   经过优化,我们在书写ListView的时候仅仅需要把这两个工具类导入到项目,自己仅仅需要书写适配器的构造方法以及convert()方法即可,这样优化了ListView的执行效率

------------------------------------------------------------------------------------------------------------------------

【附录】

源代码下载链接:https://github.com/monsterLin/WeiBo_Looking