如何实现视图持有者?
我正在使用视图保持器从动态数组适配器中显示。它可以工作,但当我滚动列表时,显示的数据会不规则地变化。我希望我的列表视图仅填充一次,而不是每次滚动时都填充我的清单。 有什么建议吗? 这是
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.sample, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.name = (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();
}
// Bind the data efficiently with the holder.
if(_first==true)
{
if(id<myElements.size())
{
holder.name.setText(myElements.get(id));
holder.icon.setImageBitmap( mIcon1 );
id++;
}
else
{
_first=false;
}
}
//holder.icon.setImageBitmap(mIcon2);
/*try{
if(id<myElements.size())
id++;
else
{
id--;
}
}
catch(Exception e)
{
android.util.Log.i("callRestService",e.getMessage());
}*/
return convertView;
}
static class ViewHolder {
TextView name;
ImageView icon;
}
加载列表时我的代码,如下所示: https://i.sstatic.net/NrGhR .png 滚动一些数据后https://i.sstatic.net/sMbAD.png 它看起来像这样,如果我滚动到开头,它看起来再次 https://i。 sstatic.net/0KjMa.png
PS:我的列表必须按字母顺序排列
i am using a viewholder to display from a dynamic arrayadapter.it works but the data displayed changes irregularly when i scroll the List.i want my List View to be populated only once ,Not all the time when i scroll my list. Any suggestion?
Here is my Code
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.sample, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.name = (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();
}
// Bind the data efficiently with the holder.
if(_first==true)
{
if(id<myElements.size())
{
holder.name.setText(myElements.get(id));
holder.icon.setImageBitmap( mIcon1 );
id++;
}
else
{
_first=false;
}
}
//holder.icon.setImageBitmap(mIcon2);
/*try{
if(id<myElements.size())
id++;
else
{
id--;
}
}
catch(Exception e)
{
android.util.Log.i("callRestService",e.getMessage());
}*/
return convertView;
}
static class ViewHolder {
TextView name;
ImageView icon;
}
when the list is loaded it looks like this : https://i.sstatic.net/NrGhR.png after scrolling some data https://i.sstatic.net/sMbAD.png it looks like this, and again if i scroll to the beginning it looks https://i.sstatic.net/0KjMa.png
P.S : my list have to be in alphabetic order
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你试过这个吗?
如果是的话,有什么问题吗?
我认为一次加载所有行不是一个好主意。您最终会在内存中拥有大量无用的视图,这些视图会毫无意义地减慢应用程序的速度。
视图和视图上的操作(如 inflate、findViewById、getChild..)非常昂贵,您应该尝试尽可能地重用它们。这就是我们使用 ViewHolder 的原因。
Have you tried this?
If yes, what's wrong with it?
I don't think loading all the rows at once is a good idea. You will end up having plenty of useless Views in memory that are going to slow the application down for nothing.
Views and operations on views (like inflate, findViewById, getChild..) are expensive, you should try to reuse them as much as possible. That's why we use ViewHolders.
您需要编写自己的
ListView
版本才能做到这一点(这很糟糕)。如果ListView
无法正常工作,则可能意味着您做错了什么。id
元素来自哪里?您将在getView()
方法中获取位置,因此无需担心超出列表范围。该位置链接到列表中的元素位置,因此您可以获得正确的元素,如下所示:当列表中的数据更改时,您可以调用此:
这将使用新数据重建列表(同时保持滚动和内容) )。
You would need to write you own version of
ListView
to do that (which is bad). If theListView
doesn't work properly, it probably means that you are doing something wrong.Where does the
id
element come from? You are getting the position in yourgetView()
method, so you don't need to worry about exceeding list bounds. The position is linked to the element position in your list, so you can get the correct element like this:When the data in your list changes, you can call this:
That will rebuild your list with new data (while keeping your scrolling and stuff).