好吧,我使用自定义ListView,列表视图有202个项目。加载ListView时,需要花费太多时间。大约6~7秒。我在getView()方法中使用白色System.out.println。然后getView运行202次,之后getView再次运行202次。有一段时间,getView运行 循环操作5次。 这是我的listview布局xml
<ScrollView
android:id="@+id/djrwxm_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<com.joinbright.util.NoScrollListView
android:id="@+id/djrwxm_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:dividerHeight="0dip"
android:focusable="false"
android:focusableInTouchMode="true"
android:footerDividersEnabled="false"
android:listSelector="#00000000"
android:padding="1dip" />
</ScrollView>
这是我的自定义ListView java类
public class NoScrollListView extends ListView
{
public NoScrollListView(Context context)
{
super(context);
}
public NoScrollListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public NoScrollListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expanSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expanSpec);
}
}
我的自定义适配器代码
public class ListViewAdapter extends BaseAdapter
{
List<Bundle> newsItems;
// private LayoutInflater mInflater;
public Bundle map = new Bundle();
private List<String> spinnerList = new ArrayList<String>(3);
private List<String> clfsspinnerList = new ArrayList<String>(3);
private ArrayAdapter<String> djjladapter;
private ArrayAdapter<String> clfsadapter;
private int index = -1;
private boolean ifload = true;
public ListViewAdapter(List<Bundle> cList)
{
this.newsItems = cList;
spinnerList.add("");
spinnerList.add("zc");
spinnerList.add("yc");
clfsspinnerList.add("");
clfsspinnerList.add("wxcl");
clfsspinnerList.add("jdcl");
clfsspinnerList.add("zrqx");
// mInflater = (LayoutInflater) DjrwxmActivity.this
// .getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
System.out.println("getCount()::::"+newsItems.size());
return newsItems.size();
}
@Override
public Object getItem(int position)
{
System.out.println("getItem()::::");
return newsItems.get(position);
}
@Override
public long getItemId(int position)
{
System.out.println("getItemId()::::");
return position;
}
这是我的getView方法
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
int type = getItemViewType(position);
System.out.println("//******************************************************************* //");
System.out.println("getView =========:::" + position + "convertView:: " + convertView + " type = " + type);
ViewHolder holder = null;
try {
map = newsItems.get(position);
if (convertView == null) {
// convertView = mInflater.inflate(R.layout.listitem_djrwxm,
// null);
convertView = getLayoutInflater().inflate(R.layout.listitem_djrwxm,
null);
holder = new ViewHolder();
holder.djrwxm_sbguid = (TextView) convertView
.findViewById(R.id.djrwxm_sbguid);
holder.djrwxm_djzq = (TextView) convertView
.findViewById(R.id.djrwxm_djzq);
holder.djrwxm_sfdj = (TextView) convertView
.findViewById(R.id.djrwxm_sfdj);
holder.djrwxm_guid = (TextView) convertView
.findViewById(R.id.djrwxm_guid);
holder.djrwxm_djdx = (TextView) convertView
.findViewById(R.id.djrwxm_djdx);
holder.djrwxm_sbmc = (TextView) convertView
.findViewById(R.id.djrwxm_sbmc);
holder.djrwxm_rwzt = (TextView) convertView
.findViewById(R.id.djrwxm_rwzt);
holder.djrwxm_djnr = (TextView) convertView
.findViewById(R.id.djrwxm_djnr);
holder.djrwxm_djbz = (TextView) convertView
.findViewById(R.id.djrwxm_djbz);
holder.djrwxm_djjl = (Spinner) convertView
.findViewById(R.id.djrwxm_djjl);
holder.djrwxm_ycsm = (EditText) convertView
.findViewById(R.id.djrwxm_ycsm);
holder.djrwxm_cljg = (EditText) convertView
.findViewById(R.id.djrwxm_cljg);
holder.djrwxm_clfs = (Spinner) convertView
.findViewById(R.id.djrwxm_clfs);
holder.djrwxm_bz = (EditText) convertView
.findViewById(R.id.djrwxm_bz);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
djjladapter = new ArrayAdapter<String>(DjrwxmActivity.this,
android.R.layout.simple_spinner_item, spinnerList);
djjladapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
clfsadapter = new ArrayAdapter<String>(DjrwxmActivity.this,
android.R.layout.simple_spinner_item, clfsspinnerList);
clfsadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.djrwxm_djjl.setAdapter(djjladapter);
holder.djrwxm_clfs.setAdapter(clfsadapter);
holder.djrwxm_djjl.setPrompt("请选择点检结论:");
holder.djrwxm_djjl.setSelection(1);
holder.djrwxm_clfs.setPrompt("请选择处理方式:");
final ViewHolder queholder = holder;
holder.djrwxm_djjl
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(
android.widget.AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
if (arg2 == 1) {
queholder.djrwxm_clfs.setClickable(false);
queholder.djrwxm_ycsm.setEnabled(false);
} else {
queholder.djrwxm_clfs.setClickable(true);
queholder.djrwxm_ycsm.setEnabled(true);
}
// arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(
android.widget.AdapterView<?> arg0) {
// arg0.setVisibility(View.VISIBLE);
}
});
holder.djrwxm_clfs.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case 0:
Spinner ss = (Spinner) v;
ss.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
String sbmc = newsItems.get(position)
.getString("sbmc").toString();
String djdx = newsItems.get(position)
.getString("djdx").toString();
String sbguid = newsItems.get(position)
.getString("sbguid").toString();
@Override
public void onItemSelected(
AdapterView<?> parent, View view,
int position, long id) {
Intent i = getIntent();
String rwid = i.getStringExtra("rwid");
if (position == 3) {
Intent intent = new Intent(
DjrwxmActivity.this,
DjqxActivity.class);
intent.putExtra("sbmc", sbmc);
// intent.putExtra("djdx", djdx);
intent.putExtra("sbguid", sbguid);
intent.putExtra("rwid", rwid);
startActivity(intent);
overridePendingTransition(
R.anim.zoom_enter,
R.anim.zoom_exit);
}
}
@Override
public void onNothingSelected(
AdapterView<?> parent) {
}
});
break;
}
return false;
}
});
sitbopedjrwxmdao = new sITbOpeDjrwxmDaoImpl(DjrwxmActivity.this);
Intent intent = getIntent();
String rwid = intent.getStringExtra("rwid");
String djdx = map.getString("djdx").toString();
Bundle bundle = sitbopedjrwxmdao.getneirong(djdx, rwid);
holder.djrwxm_sbguid.setText(map.getString("sbguid").toString());
holder.djrwxm_djzq.setText(map.getString("djzq").toString());
holder.djrwxm_sfdj.setText(map.getString("sfdj").toString());
holder.djrwxm_guid.setText(map.getString("guid").toString());
holder.djrwxm_djdx.setText(map.getString("djdx").toString());
holder.djrwxm_sbmc.setText(map.getString("sbmc").toString());
holder.djrwxm_rwzt.setText(map.getString("rwzt").toString());
holder.djrwxm_djnr.setText(map.getString("djnr").toString());
holder.djrwxm_djbz.setText(map.getString("djbz").toString());
if (bundle != null) {
if ("1600801".equals(bundle.getString("djjl"))) {
holder.djrwxm_djjl.setSelection(1);
} else if ("1600802".equals(bundle.getString("djjl"))) {
holder.djrwxm_djjl.setSelection(2);
}
holder.djrwxm_ycsm.setText(bundle.getString("ycsm"));
holder.djrwxm_cljg.setText(bundle.getString("cljg"));
holder.djrwxm_bz.setText(bundle.getString("bz"));
if ("1601601".equals(bundle.getString("clfs"))) {
holder.djrwxm_clfs.setSelection(2);
} else if ("1601602".equals(bundle.getString("clfs"))) {
holder.djrwxm_clfs.setSelection(1);
} else if ("1601603".equals(bundle.getString("clfs"))) {
holder.djrwxm_clfs.setSelection(3);
}
}
final String djbz = holder.djrwxm_djbz.getText().toString();
holder.djrwxm_djbz.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationInWindow(location);
int x = location[0];
int y = location[1];
Bundle bundle = new Bundle();
bundle.putString("djbz", djbz);
bundle.putInt("x", x);
bundle.putInt("y", y);
showDialog(1, bundle);
}
});
holder.djrwxm_cljg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
index = position;
}
return false;
}
});
if (index != -1 && index == position) {
holder.djrwxm_cljg.requestFocus();
}
if (position == AdapterView.INVALID_POSITION) {
} else {
if (position % 2 == 0) {
convertView
.setBackgroundResource(R.xml.xml_list_shape_blue);
} else {
convertView
.setBackgroundResource(R.xml.xml_list_shape_hui);
}
}
// } catch (Exception e) {
// e.printStackTrace();
// }
if (!ifload) {
ifload = true;
}
return convertView;
}
}
最后一个是我的logcat out
11-05 17:20:32.800: I/System.out(20354): 调用了getCount()::::202
11-05 17:20:32.800: I/System.out(20354): 调用了getCount()::::202
11-05 17:20:32.831: I/System.out(20354): 调用了getCount()::::202
11-05 17:20:32.831: I/System.out(20354): 调用了getCount()::::202
11-05 17:20:32.831: I/System.out(20354): //*******************************************************************//
11-05 17:20:32.831: I/System.out(20354): getView =========:::0convertView:: null type = 0
11-05 17:20:32.901: I/System.out(20354): //*******************************************************************//
11-05 17:20:32.901: I/System.out(20354): getView =========:::1convertView:: android.widget.TableLayout{41dc55b8 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:32.956: I/System.out(20354): //*******************************************************************//
11-05 17:20:32.956: I/System.out(20354): getView =========:::2convertView:: android.widget.TableLayout{41dc6048 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:33.003: I/System.out(20354): //*******************************************************************//
11-05 17:20:33.003: I/System.out(20354): getView =========:::3convertView:: android.widget.TableLayout{41dc9770 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:33.050: I/System.out(20354): //*******************************************************************//
11-05 17:20:33.050: I/System.out(20354): getView =========:::4convertView:: android.widget.TableLayout{41dcc7a0 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:33.097: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.105: I/System.out(20354): getView =========:::198convertView:: android.widget.TableLayout{42aa8ad0 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.144: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.144: I/System.out(20354): getView =========:::199convertView:: android.widget.TableLayout{42ab8928 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.183: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.183: I/System.out(20354): getView =========:::200convertView:: android.widget.TableLayout{42ac89a0 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.230: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.230: I/System.out(20354): getView =========:::201convertView:: android.widget.TableLayout{42ad8b38 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.269: I/System.out(20354): 调用了getCount()::::202
11-05 17:20:42.269: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.269: I/System.out(20354): getView =========:::0convertView:: android.widget.TableLayout{42ae8cd0 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.339: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.339: I/System.out(20354): getView =========:::1convertView:: android.widget.TableLayout{41dc55b8 V.E..... ......ID 0,0-0,0} type = 0
11-05 17:20:42.394: I/System.out(20354): //*******************************************************************//
11-05 17:20:42.394: I/System.out(20354): getView =========:::2convertView:: android.widget.TableLayout{41dc6048 V.E..... ......ID 0,0-0,0} type = 0
答案 0 :(得分:1)
ListView
内有ScrollView
。这是不好的。 ListView
已经知道如何滚动,因此存在冲突。要让周围的ScrollView
知道如何滚动,需要询问ListView
整个内容的大小。要让ListView
了解整个内容的大小,就需要在每个元素上调用getView()
。你绝对不想这样做。