listview观看者模式.在删除项目的位置设置标签的位置.安卓 [英] listview viewholder pattern. where to settag for position on removing an item. android

查看:89
本文介绍了listview观看者模式.在删除项目的位置设置标签的位置.安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到viewholder模式对于平滑滚动非常有用,但是我遇到了一个错误.如果从列表视图中删除一项,则标签有些混乱.因此,删除第二个项目不会删除用户单击的项目,而是删除先前设置了标签的项目.在下面的代码示例中,我在哪里为按钮的位置设置了标记,还是破坏了Viewholder范式?有没有人有更好的方法来删除项目?你怎么做呢?示例:

I see that the viewholder pattern is good for smooth scrolling, but I have ran into an error with the pattern. If one deletes an item from the listview then the tags are are a little messed up. Therefore, deleting a second item doesn't delete the one an user clicked on, but deletes whichever the previous tag was set on. In the code example below, is where I have set the tag for the button's position okay or does it break the viewholder paradigm? Does anyone have a better way of removing items? How do you do it? Example:

public View getView ( int position, View convertView, ViewGroup parent )
    {
        View v = convertView;
        ViewHolder holder;
        if( v == null )
        {
            LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.fpc_contents_row, null);
            holder = new ViewHolder();
            holder.delete   = (Button)  v.findViewById( R.id.row_delete );
            v.findViewById( R.id.fpc_contents_row_delete ).setOnClickListener( global_onClickListener );
                    //used to set position tag here.  (deleted wrong items after the first delete)
                    //i.e)  holder.delete.setTag( position );
            v.setTag( holder );
        }
        else
            holder = (ViewHolder) v.getTag();
        //setting the tag here then delete functionality works just fine.
        holder.delete.setTag( position );
        holder.name.setText( fpcItems.get(position).item0.name );
        return v;
    }

final  OnClickListener global_onClickListener = new OnClickListener() 
    {
        public void onClick( final View view ) 
        {
            switch( view.getId() ) 
            {
            case R.id.fpc_contents_row_delete :
                int position = (Integer) view.getTag();
                arraylist.remove(position);
                notifyDataSetChanged();                                
                break;
            }
        }
    };

推荐答案

是的,如果要在容器中的对象上设置标签,则需要在(v == null)有条件的条件下进行操作.

Yes, if you're going to set the tag on an object in the holder, you'll need to do it outside of your (v == null) conditional.

如果不这样做,该标签将仅在新视图上设置,而不在回收视图上设置.例如,如果向下三行的列表项使用的是回收视图而不是夸大一个全新的视图,则它将具有最初创建该视图时所使用的任何位置的位置标签.

If you don't, the tag will only be set on new views, not on recycled ones. If, say, a list item three rows down uses a recycled view instead of inflating a brand new one, it will have the position tag of whatever position was used when the view was originally created.

您的解决方法是正确的:请确保在调用holder = (ViewHolder)v.getTag()之后调用所有设置的方法.

Your fix is correct: make sure that you call all of your set methods AFTER you do your holder = (ViewHolder)v.getTag() call.

这篇关于listview观看者模式.在删除项目的位置设置标签的位置.安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆