Android 为什么ListView的滚动不一致?
我目前正在开发一个Android 为什么ListView的滚动不一致?,android,listview,scroll,lag,Android,Listview,Scroll,Lag,我目前正在开发一个列表视图,它包含大约80到100个项目(文本视图)。我不认为内容太多,但当我(用手指)滚动时,ListView会跳动或滞后。但是,当我使用“快速滚动按钮”(即列表视图右侧的按钮)时,滚动看起来非常一致和平滑 有人有同样的问题吗?我在HTC Sensation上测试了ListView 以下是我的ListView代码: <ListView android:id="@+id/list_view" android:layout_height="match_pa
列表视图
,它包含大约80到100个项目(文本视图
)。我不认为内容太多,但当我(用手指)滚动时,ListView
会跳动或滞后。但是,当我使用“快速滚动按钮”(即列表视图右侧的按钮)时,滚动看起来非常一致和平滑
有人有同样的问题吗?我在HTC Sensation上测试了ListView
以下是我的ListView代码:
<ListView
android:id="@+id/list_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollingCache="true">
</ListView>
Java代码:
adptr = new ArrayAdapter<String>(iF, R.layout.list_item, showing) {
@Override
public View getView(int position, View convertView, ViewGroup grp) {
LinearLayout lin = new LinearLayout(this.getContext());
lin.setOrientation(LinearLayout.HORIZONTAL);
// Icon
ImageView v = new ImageView(this.getContext());
// v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon));
// Text
TextView txt = new TextView(this.getContext());
txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
txt.setPadding(10, 10, 10, 10);
txt.setText(this.getItem(position));
txt.setTextColor(getLineColor(position));
// Shortcut
LinearLayout shortLin = new LinearLayout(this.getContext());
shortLin.setGravity(Gravity.RIGHT);
LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
shortLin.setLayoutParams(par);
TextView s = new TextView(this.getContext());
s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
s.setPadding(10, 10, 10, 10);
s.setText(getShortcut(position));
shortLin.addView(s);
// Return
txt.invalidate();
v.invalidate();
s.invalidate();
lin.addView(v);
lin.addView(txt);
lin.addView(shortLin);
return lin;
}
};
adptr=新阵列适配器(如果,R.layout.list_项,显示){
@凌驾
公共视图getView(int位置、视图转换视图、视图组grp){
LinearLayout lin=新的LinearLayout(this.getContext());
林。设置方向(线性布局。水平);
//图标
ImageView v=新的ImageView(this.getContext());
//v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_图标));
//正文
TextView txt=新的TextView(this.getContext());
setTextSize(Float.valueOf(prefs.getString(“pref_txtSize”,“12”)));
setPadding(10,10,10,10);
txt.setText(this.getItem(position));
setTextColor(getLineColor(position));
//捷径
LinearLayout shortLin=新的LinearLayout(this.getContext());
shortLin.setGravity(Gravity.RIGHT);
LayOutPARAMPAR=新LayoutParams(LayOutPARAM.MatCHYPrimes,LayOutPARAM.WrAPHY内容);
shortLin.setLayoutParams(par);
TextView s=新的TextView(this.getContext());
s、 setTextSize(Float.valueOf(prefs.getString(“pref_txtSize”,“12”)));
s、 setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
s、 设置填充(10,10,10,10);
s、 setText(获取快捷方式(位置));
shortLin.addView(s);
//返回
txt.invalidate();
v、 使无效();
s、 使无效();
林·阿德维尤(v);
lin.addView(txt);
lin.addView(shortLin);
返回林;
}
};
如您所见,我制作了一个定制的列表视图
。ArrayAdapter
将以不同的方法添加(此处未显示)
提前谢谢
阿德里安你还记得缓存吗
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater
.inflate(R.layout.list_view_home_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view
.findViewById(R.id.textView);
holder.title.setText("blah");
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
及
你还记得缓存吗
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater
.inflate(R.layout.list_view_home_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view
.findViewById(R.id.textView);
holder.title.setText("blah");
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
及
?每次拉取视图时,您都在为列表动态创建每个视图
元素。这可能是有史以来最低效的机制:)
第一步,看看是否可以用XML布局视图。使事情更容易管理
即使不这样做,也要使用convertView
参数。它存在的唯一原因是尽可能避免重新分配视图。如果convertView
为非空,则它具有您在先前调用getView()
时创建的所有视图。您所要做的就是在每个视图中填写适当的信息(在您的例子中,基本上是setText()
调用)。如果convertView
为空,请创建视图
另外,不要使()无效。
。至少,在没有XML布局的情况下,这里有一个重写的版本应该快一点
@Override
public View getView(int position, View convertView, ViewGroup grp) {
if(convertView == null) {
LinearLayout lin = new LinearLayout(this.getContext());
lin.setOrientation(LinearLayout.HORIZONTAL);
// Icon
ImageView v = new ImageView(this.getContext());
// v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon));
// Text
TextView txt = new TextView(this.getContext());
txt.setId(1);
txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
txt.setPadding(10, 10, 10, 10);
txt.setTextColor(getLineColor(position));
// Shortcut
LinearLayout shortLin = new LinearLayout(this.getContext());
shortLin.setGravity(Gravity.RIGHT);
LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
shortLin.setLayoutParams(par);
TextView s = new TextView(this.getContext());
s.setId(2);
s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
s.setPadding(10, 10, 10, 10);
shortLin.addView(s);
lin.addView(v);
lin.addView(txt);
lin.addView(shortLin);
}
TextView txt = (TextView)convertView.findViewById(1);
txt.setText(this.getItem(position));
TextView txt = (TextView)convertView.findViewById(2);
txt.setText(getShortcut(position));
return lin;
}
同样,这不是最好的方法或最佳实践,但这应该是可行的。每次拉取视图时,您都会动态地为列表创建每个
视图
元素。这可能是有史以来最低效的机制:)
第一步,看看是否可以用XML布局视图。使事情更容易管理
即使不这样做,也要使用convertView
参数。它存在的唯一原因是尽可能避免重新分配视图。如果convertView
为非空,则它具有您在先前调用getView()
时创建的所有视图。您所要做的就是在每个视图中填写适当的信息(在您的例子中,基本上是setText()
调用)。如果convertView
为空,请创建视图
另外,不要使()无效。。至少,在没有XML布局的情况下,这里有一个重写的版本应该快一点
@Override
public View getView(int position, View convertView, ViewGroup grp) {
if(convertView == null) {
LinearLayout lin = new LinearLayout(this.getContext());
lin.setOrientation(LinearLayout.HORIZONTAL);
// Icon
ImageView v = new ImageView(this.getContext());
// v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon));
// Text
TextView txt = new TextView(this.getContext());
txt.setId(1);
txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
txt.setPadding(10, 10, 10, 10);
txt.setTextColor(getLineColor(position));
// Shortcut
LinearLayout shortLin = new LinearLayout(this.getContext());
shortLin.setGravity(Gravity.RIGHT);
LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
shortLin.setLayoutParams(par);
TextView s = new TextView(this.getContext());
s.setId(2);
s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth));
s.setPadding(10, 10, 10, 10);
shortLin.addView(s);
lin.addView(v);
lin.addView(txt);
lin.addView(shortLin);
}
TextView txt = (TextView)convertView.findViewById(1);
txt.setText(this.getItem(position));
TextView txt = (TextView)convertView.findViewById(2);
txt.setText(getShortcut(position));
return lin;
}
同样,这不是最佳方法或最佳实践,但这应该有效。最后,我提出了以下解决方案:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) iF.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.item_text);
holder.shortcut = (TextView) view.findViewById(R.id.item_short);
holder.title.setId(1);
holder.shortcut.setId(2);
holder.title.setText(getItem((position)));
holder.shortcut.setText(getShortcut(position));
holder.title.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
holder.shortcut.setTextSize(Float.valueOf(prefs.getString("prefs_txtSize", "12")));
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
TextView title = (TextView) convertView.findViewById(1);
title.setText(getItem(position));
TextView shortcut = (TextView) convertView.findViewById(2);
shortcut.setText(getShortcut(position));
}
return view;
}
我现在真的很高兴它成功了。现在是00:33,所以我想我该睡觉了。今天(昨天)有足够的工作
感谢所有的支持最后,我提出了以下解决方案:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) iF.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.item_text);
holder.shortcut = (TextView) view.findViewById(R.id.item_short);
holder.title.setId(1);
holder.shortcut.setId(2);
holder.title.setText(getItem((position)));
holder.shortcut.setText(getShortcut(position));
holder.title.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12")));
holder.shortcut.setTextSize(Float.valueOf(prefs.getString("prefs_txtSize", "12")));
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
TextView title = (TextView) convertView.findViewById(1);
title.setText(getItem(position));
TextView shortcut = (TextView) convertView.findViewById(2);
shortcut.setText(getShortcut(position));
}
return view;
}
我现在真的很高兴它成功了。现在是00:33,所以我想我该睡觉了。今天(昨天)有足够的工作
感谢所有的支持不确定是否必须在getView()中始终使文本视图无效是的。这可能不是必要的。我真的不知道它是干什么的。在一些谷歌研究中发现:)不确定是否必须在getView()中始终使文本视图无效是的。这可能不是必要的。我真的不知道它是干什么的。在谷歌的一些研究中发现:)谢谢你的回答。我从来没有听说过更平坦的地方。我将转到android开发页面,阅读更多关于这方面的内容。为了补充这个答案,在ListView上观看一个很棒的Google IO事件的屏幕广播将是一件很好的事情。它帮助我记住了在这个网站上检查好答案的方法!;-)我昨天也有同样的问题。我研究了充气机的漏洞!现在,最后一个问题:我在哪里执行setTextSize()?我必须更新它,这样我就不能在xml中创建它,因为用户可以在首选项中更改它。像设置文本一样使用它(holder.title.setText(“blah”)),但请记住cachcing(当您不正确保存它时,您将在刷卡后丢失它),因此它将是holder.s.setTextSize(Float.valueOf(prefs.g