Let’s start by reviewing traditional custom adapters
(1) Create a BaseAdaper subclass and override the three methods
int getCount();
Object getItem(int position)
View getView(int Position, View convertVew, View parent
(2) Customize an item layout
(3) For View reuse and optimization, use getView only for the first time and create ConverView
Create a cache class ViewHolder that contains the required Item controls
ConvertView == NULL create ViewHolder using setTag ()
Using getView again, directly using getTag, get the cache object Viewholder.
——————————————————————————————————————
The traditional Adapter “:
<span style="font-size:24px;">p</span><span style="font-size:18px;">ackage com.example.baseAdapter;
import java.util.List;
import com.example.magicAdapter.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Bean> mDatas;
public MyAdapter(Context context, List<Bean> mDatas) {
mInflater = LayoutInflater.from(context);
this.mDatas = mDatas;
}
@Override
public int getCount(a) {
return mDatas.size();
}
@Override
public Bean 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 viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_layout, null);
viewHolder = new ViewHolder();
viewHolder.item_title = (TextView) convertView.findViewById(R.id.item_title);
viewHolder.item_desc = (TextView) convertView.findViewById(R.id.item_desc);
viewHolder.item_time = (TextView) convertView.findViewById(R.id.item_time);
viewHolder.item_phone = (TextView) convertView.findViewById(R.id.item_phone);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
/ / data
viewHolder.item_title.setText(getItem(position).getTitle());
viewHolder.item_desc.setText(getItem(position).getDesc());
viewHolder.item_time.setText(getItem(position).getTime());
viewHolder.item_phone.setText(getItem(position).getPhone());
return convertView;
}
/** * Cache class */
class ViewHolder {
TextView item_title;
TextView item_desc;
TextView item_time;
TextView item_phone;
}
}</span><span style="font-size:24px;">
</span>
Copy the code
The most important getView: extract the actual time ViewHolder extracts:
Let’s look at the traditional Adapter getView prime judgment convertView== null
To choose is:
(1) Create a new viewHolder and store the Item layout control to the viewHolder ==> convrtView== NULL
Get convertView from LayoutInflater. From (layoutId, parent, false);
(b) Retrieve the item control and store it in the viewHolder,
(c) Set the Viewholder tag, convertView.setTag(Viewholder)
(2) Get viewHolder by getTag() === > convertView! = null
viewHolder = convertView.getTag();
Set the values of the individual controls via viewHolder
\
public View getView(int position, View convertView, ViewGroup parent) {
.
} \
\
\
Taking ViewHolder
Create a MagicViewHolder
SparseArray< view > mViews; SparseArray< view > mViews;
Layout file
int layoutId
To get the layout View –>LayoutInflater. From (Context).inflate(layoutId, parent, false)
Context context,
ViewGroup parent
The item location
int position
\
ConvertView reusability improves efficiency in capturing ViewHolder
To determine if convertView is empty, fetch the ViewHolder by tag
Provides a method to get a ViewHolder
\
Retrieve the control in item by ViewHolder according to SparseArray<View>
\
The ViewHolder returns a convertVeiw,
GetView for the adapter returns a convertView
/*** * returns convertView *@return* /
public View getConvertView(a) {
return contertView;
}
Copy the code
Here is the MagicViewHolder to be extracted and created:
package com.example.utils;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* ViewHolder类
*
* layoutID, ---> itemView --->
*/
public class MagicViewHolder {
/* * The container that stores the controls in itemView */ is stored with the VIEW ID and the view key-value pair
private SparseArray<View> mViews;
/** * Specifies the VIew as a child * note that the VIew is reused each time getView() is called, and position is changed */
private int position;
/** * item layout */
private View contertView;
public MagicViewHolder(Context context, ViewGroup parent,
int position, int layoutId){
this.position = position;
this.mViews = new SparseArray<View>();
contertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
contertView.setTag(this);
}
/** * get ViewHolder *@param context
* @param convertView
* @param parent
* @paramLayoutId item layoutId *@paramPosition item Position *@return* /
public static MagicViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position){
if(convertView == null) {return new MagicViewHolder(context, parent, position, layoutId);
}else {
return(MagicViewHolder) convertView.getTag(); }}/*** * returns convertView *@return* /
public View getConvertView(a) {
return contertView;
}
/** * Get the view of each control in item via viewId * use generics, return subclasses of View *@return* /
public <T extends View> T getView(int viewId){
View view = mViews.get(viewId);
if(view == null){
view = contertView.findViewById(viewId);
mViews.put(viewId, view);
}
return(T) view; }}Copy the code
\
Extracted Adapter:
<span style="font-size:18px;">package com.example.utils;
import java.util.List;
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 com.example.baseAdapter.Bean;
import com.example.magicAdapter.R;
public class MagicAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private Context context;
private List<Bean> mdatas;
public MagicAdapter(Context context, List<Bean> mdatas) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.mdatas = mdatas;
}
@Override
public int getCount(a) {
// TODO Auto-generated method stub
return mdatas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mdatas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MagicViewHolder magicViewHolder =
MagicViewHolder.get(context, convertView,
parent, R.layout.item_layout, position);
// Get data
Bean bean = (Bean) getItem(position);
// Get the specified view via magicViewHolder and viewid in item and set the value
((TextView)(magicViewHolder.getView(R.id.item_title))).setText(bean.getTitle());
((TextView)(magicViewHolder.getView(R.id.item_desc))).setText(bean.getDesc());
((TextView)(magicViewHolder.getView(R.id.item_time))).setText(bean.getTime());
((TextView)(magicViewHolder.getView(R.id.item_phone))).setText(bean.getPhone());
return magicViewHolder.getConvertView();
}
}</span>
Copy the code
\
\
\
\
\