android listview 多个item 倒计时怎么做好
2个回答
展开全部
1. Adapter.getView()
public View getView(int position, View convertView, ViewGroup
parent){...}
这个方法就是用来获得指定位置要显示的View。官网解释如下:
Get a View that displays the data at the specified position in the data
set. You can either create a View manually or inflate it from an XML layout
file.
当要显示一个View就调用一次这个方法。这个方法是ListView性能好坏的关键。方法中有个convertView,这个是Android在为我们而做的缓存机制。
ListView中每个item都是通过getView返回并显示的,假如item有很多个,那么重复创建这么多对象来显示显然是不合理。因此,Android提供了Recycler,将没有正在显示的item放进RecycleBin,然后在显示新视图时从RecycleBin中复用这个View。
Recycler的工作原理大致如下:
假设屏幕最多能看到11个item,那么当第1个item滚出屏幕,这个item的View进入RecycleBin中,第12个要出现前,通过getView从回收站(RecycleBin)中重用这个View,然后设置数据,而不必重新创建一个View。
我们用Android提供的APIDemos来验证这个过程:
先看关键代码:
public View getView(int position, View convertView, ViewGroup parent)
{
// A ViewHolder keeps references to children views to avoid unneccessary
calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no
need
// to reinflate it. We only inflate a new View when the convertView
supplied
// by ListView is null.
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
Log.v("tag", "positon "+position+" convertView is null, "+"new:
"+convertView);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else
{
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
Log.v("tag", "positon "+position+" convertView is not null,
"+convertView);
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder
{
TextView text;
ImageView icon;
}
public View getView(int position, View convertView, ViewGroup
parent){...}
这个方法就是用来获得指定位置要显示的View。官网解释如下:
Get a View that displays the data at the specified position in the data
set. You can either create a View manually or inflate it from an XML layout
file.
当要显示一个View就调用一次这个方法。这个方法是ListView性能好坏的关键。方法中有个convertView,这个是Android在为我们而做的缓存机制。
ListView中每个item都是通过getView返回并显示的,假如item有很多个,那么重复创建这么多对象来显示显然是不合理。因此,Android提供了Recycler,将没有正在显示的item放进RecycleBin,然后在显示新视图时从RecycleBin中复用这个View。
Recycler的工作原理大致如下:
假设屏幕最多能看到11个item,那么当第1个item滚出屏幕,这个item的View进入RecycleBin中,第12个要出现前,通过getView从回收站(RecycleBin)中重用这个View,然后设置数据,而不必重新创建一个View。
我们用Android提供的APIDemos来验证这个过程:
先看关键代码:
public View getView(int position, View convertView, ViewGroup parent)
{
// A ViewHolder keeps references to children views to avoid unneccessary
calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no
need
// to reinflate it. We only inflate a new View when the convertView
supplied
// by ListView is null.
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
Log.v("tag", "positon "+position+" convertView is null, "+"new:
"+convertView);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else
{
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
Log.v("tag", "positon "+position+" convertView is not null,
"+convertView);
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder
{
TextView text;
ImageView icon;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询