package zhangphil.demo;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private int tagId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter mAdapter = new ArrayAdapter(this, 0) {
private TextView text1;
private TextView text2;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_2, null);
//convertView.setMinimumHeight(400);
Log.d("位置" + position, "创建新convertView,设置tagId:" + tagId);
convertView.setTag(tagId++);
} else {
Log.d("位置" + position, convertView.getTag() + " 复用convertView");
}
text1 = convertView.findViewById(android.R.id.text1);
text1.setTextColor(getResources().getColor(android.R.color.holo_blue_bright));
text1.setText(getItem(position));
text2 = convertView.findViewById(android.R.id.text2);
text2.setTextColor(getResources().getColor(android.R.color.holo_red_light));
text2.setText("convertView tagId:" + String.valueOf(convertView.getTag()));
return convertView;
}
@Nullable
@Override
public String getItem(int position) {
return "位置position:" + position;
}
@Override
public int getCount() {
return 99999;
}
};
setListAdapter(mAdapter);
}
}
定义了一个tagId,用以标记convertView,在新创建的convertView里面打入进去,然后在后续convertView复用时候取出来,以追踪此时复用的到底是哪个convertView。
代码运行结果:
logcat日志输出结果:
11-03 14:40:08.745 22766-22766/zhangphil.demo D/位置0: 创建新convertView,设置tagId:0
11-03 14:40:08.760 22766-22766/zhangphil.demo D/位置1: 创建新convertView,设置tagId:1
11-03 14:40:08.765 22766-22766/zhangphil.demo D/位置2: 创建新convertView,设置tagId:2
11-03 14:40:08.773 22766-22766/zhangphil.demo D/位置3: 创建新convertView,设置tagId:3
11-03 14:40:08.782 22766-22766/zhangphil.demo D/位置4: 创建新convertView,设置tagId:4
11-03 14:40:08.787 22766-22766/zhangphil.demo D/位置5: 创建新convertView,设置tagId:5
11-03 14:40:08.795 22766-22766/zhangphil.demo D/位置6: 创建新convertView,设置tagId:6
11-03 14:40:08.800 22766-22766/zhangphil.demo D/位置7: 创建新convertView,设置tagId:7
11-03 14:40:16.573 22766-22766/zhangphil.demo D/位置8: 创建新convertView,设置tagId:8
11-03 15:02:50.553 31901-31901/zhangphil.demo D/位置64: 1 复用convertView
11-03 15:02:50.637 31901-31901/zhangphil.demo D/位置65: 2 复用convertView
11-03 15:02:50.720 31901-31901/zhangphil.demo D/位置66: 5 复用convertView
11-03 15:02:50.820 31901-31901/zhangphil.demo D/位置67: 4 复用convertView
11-03 15:02:50.936 31901-31901/zhangphil.demo D/位置68: 7 复用convertView
11-03 15:02:51.069 31901-31901/zhangphil.demo D/位置69: 3 复用convertView
11-03 15:02:51.236 31901-31901/zhangphil.demo D/位置70: 8 复用convertView
11-03 15:02:51.452 31901-31901/zhangphil.demo D/位置71: 0 复用convertView
11-03 15:02:51.786 31901-31901/zhangphil.demo D/位置72: 6 复用convertView