您的位置:首页 > 其它

BaseAdapter深入学习,不规则listview,ListView加载性能优化ViewHolder

2014-11-05 22:53 543 查看
其中BaseAdapter需要重写的方法:

getCount(),getItem(int position),getItemId(int position),

getView(int position, View convertView, ViewGroup parent)

ListView加载性能优化ViewHolder
http://www.cnblogs.com/lichenwei/p/4085107.html
listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到 listView的长度,然后根据这个长度,调用getView()逐一绘制每一行。如果你的 getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方 法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文 件中inflate来的布局。我们用LayoutInflater的方法将定义好的item.xml文件提取成View实例用来显示。然后将xml文件中 的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监听 器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得要 绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个 ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那再绘制下一行,直到绘完为止。

listView中动作的监听可以参考 http://stephen830.iteye.com/blog/1141394
不规则ListView 参考 http://www.phpyuandi.com/3571.html http://blog.sina.com.cn/s/blog_629b701e0100x338.html http://www.2cto.com/kf/201310/248500.html(先看) http://www.tuicool.com/articles/MBvABz http://blog.sina.com.cn/s/blog_5da93c8f0100wx4v.html
程序 中用到Adapter如下

package com.example.weit.adapter;

import com.example.weit.R;
import com.example.weit.utils.SettingUtils;

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

public class WeitDrawerAdapter extends BaseAdapter{

private LayoutInflater mLayoutInflater;
private String [] listData;
private Context context;

private final int TYPE_COUNT=2;
private final int FIRST_TYPE=0;
private final int OTHERS_TYPE=1;
private int currentType;

/**
* @param context
* @param data
*/
public WeitDrawerAdapter(Context context,
String [] listData){
mLayoutInflater = LayoutInflater.from(context);
this.listData= listData;
this.context =context;
}
@Override
public int getCount() {
Log.i("getCount", String.valueOf(listData.length));
return listData.length;

}

@Override
public Object getItem(int position) {
if (listData!=null){
if (position>0){  //向下挪一个
return listData[position-1];
}else {
return listData[position+1];
}
}else {
return null;
}
}

@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (position==0){
return FIRST_TYPE;//第一种视图
}else {
return OTHERS_TYPE;
}
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View firstItemView =null;
View othersItemView =  null;
//得到当前 的item type
currentType = getItemViewType(position);
Log.i("getView ","currentType is "+ String.valueOf(currentType));
Log.i("getView position ",String.valueOf(position));

if (currentType == FIRST_TYPE){//如果是第一种视图
firstItemView = convertView;  //一定要写,第一次启动时初始化,后面就不再用了
FirstItemViewHolder firstItemViewHolder = null;
if (firstItemView == null){ //
Log.i("getView", "firstItemView ==  null");
firstItemView = mLayoutInflater.inflate(
R.layout.drawer_first_item, null);
firstItemViewHolder = new FirstItemViewHolder();
firstItemViewHolder.mImageView = (ImageView)firstItemView
.findViewById(R.id.drawer_userphoto);
firstItemViewHolder.mTextView = (TextView)firstItemView
.findViewById(R.id.drawer_username);

firstItemView.setTag(firstItemViewHolder);
}else {  //
Log.i("getView", "firstItemView !=null");
Log.i("getView getClass=",
firstItemView.getTag().getClass().toString());
firstItemViewHolder = (FirstItemViewHolder) firstItemView
.getTag();//此处相当于强制类型转换?
}
//设置图片和文字
firstItemViewHolder.mImageView
.setImageResource(R.drawable.ic_launcher);
firstItemViewHolder.mTextView.setText(SettingUtils.getUserFromJson(
context).getUsername());
convertView = firstItemView;
}else {//第二种视图
othersItemView = convertView;
OthersItemViewHolder othersItemViewHolder= null;
if (othersItemView == null){
Log.i("getView", "otherItemView==null");
othersItemView = mLayoutInflater.inflate(
R.layout.drawer_others_item, null);
othersItemViewHolder = new OthersItemViewHolder();
othersItemViewHolder.mTextView = (TextView)othersItemView
.findViewById(R.id.drawer_othertext);
othersItemView.setTag(othersItemViewHolder);
}else {
Log.i("getView", "othersItemView!=null");
othersItemViewHolder = (OthersItemViewHolder)convertView.getTag();
}
//设置文字
othersItemViewHolder.mTextView.setText(listData[position]);
convertView = othersItemView;
}
return convertView;
}

private class FirstItemViewHolder{
ImageView mImageView ;
TextView mTextView;
}

private class OthersItemViewHolder{
TextView mTextView;
}

}


 期中用到的布局文件如下:

先是drawer_firstitem

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="20dip" >

<com.example.weit.widget.CircleImageView
android:id="@+id/drawer_userphoto"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/abc_ic_search_api_holo_light" />

<TextView
android:id="@+id/drawer_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:paddingLeft="10dip"
android:textColor="@color/black"
android:textSize="18dip" />
</LinearLayout>

</LinearLayout>


 然后是第二个布局文件

<?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"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:id="@+id/drawer_othertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:padding="10dip"
android:textColor="@color/black"
android:text="TextView" />

</LinearLayout>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: