如何实现视图持有者?(How to implement a view holder?)

编程入门 行业动态 更新时间:2024-10-26 18:20:34
如何实现视图持有者?(How to implement a view holder?)

我使用的是一个viewholder来动态显示arrayadapter.it的作品,但是当我滚动列表时,显示的数据不规则地变化。 我希望我的列表视图只填充一次,并不是所有的时候我滚动我的列表。 任何建议? 这是我的代码

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; }

当列表加载时,它看起来像这样: http : //i.stack.imgur.com/NrGhR.png 在滚动一些数据http://i.stack.imgur.com/sMbAD.png之后 它看起来像这样,再次如果我滚动到开始,它看起来像http://i.stack.imgur.com/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 : http://i.stack.imgur.com/NrGhR.png after scrolling some data http://i.stack.imgur.com/sMbAD.png it looks like this, and again if i scroll to the beginning it looks http://i.stack.imgur.com/0KjMa.png

P.S : my list have to be in alphabetic order

最满意答案

你需要编写你自己的ListView版本来做到这一点(这很糟糕)。 如果ListView不能正常工作,这可能意味着你做错了什么。

id元素来自哪里? 您在getView()方法中获得了位置,因此您不必担心超出列表边界。 位置链接到列表中的元素位置,因此您可以像这样获取正确的元素:

myElements.get(position);

当列表中的数据发生变化时,您可以调用以下命令:

yourAdapter.notifyDataSetChanged()

这将用新数据重建你的列表(同时保持你的滚动和东西)。

You would need to write you own version of ListView to do that (which is bad). If the ListView 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 your getView() 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:

myElements.get(position);

When the data in your list changes, you can call this:

yourAdapter.notifyDataSetChanged()

That will rebuild your list with new data (while keeping your scrolling and stuff).

更多推荐

本文发布于:2023-08-07 13:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持有者   视图   如何实现   holder   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!