SparseBooleanArray - 已删除的密钥,在Listview自定义适配器中回收视图时返回

时间:2014-08-12 21:25:40

标签: android listview android-listview

我有一个自定义适配器,在每个项目视图中我都喜欢和不喜欢按钮。我正在使用SparseBooleanArray将整数映射到布尔值。在我的自定义适配器的getView中,我已经定义了这些SparseBooleanArray:

    private final SparseBooleanArray LikedPositions     = new SparseBooleanArray();
    private final SparseBooleanArray DislikedPositions  = new SparseBooleanArray();

然后,根据API检索的数据,我填写如下:

    if (Like_with_user == true){
            LikedPositions.put(position, true);
    }

    if (Dislike_with_user == true){
        DislikedPositions.put(position, true);
    }

在这一步之后,我设置了喜欢和不喜欢按钮的背景:

    if(LikedPositions.get(position)){
        holder.video_Like.setBackgroundResource(R.drawable.like_pressed);
    }else{
        holder.video_Like.setBackgroundResource(R.drawable.like_normal);
    }


    if(DislikedPositions.get(position)){
        holder.video_Dislike.setBackgroundResource(R.drawable.dislike_pressed);
    }else{
        holder.video_Dislike.setBackgroundResource(R.drawable.dislike_normal);
    }

在此之后,我将OnClickListener设置为喜欢和DisLike buutons:

    holder.video_Like.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View like) {

            int likeposition        = (Integer) like.getTag(R.string.Position);
            RelativeLayout layout   = (RelativeLayout) like.getParent();
            Button BtnLike          = (Button) layout.getChildAt(0);
            Button BtnDislike       = (Button) layout.getChildAt(1);

            if(DislikedPositions.get(likeposition)){
                BtnDislike.setBackgroundResource(R.drawable.dislike_normal);
                DislikedPositions.delete(likeposition);
                System.out.println("Dislike: "+DislikedPositions);
            }

            if(LikedPositions.get(likeposition)){
                BtnLike.setBackgroundResource(R.drawable.like_normal);
                LikedPositions.delete(likeposition);
                System.out.println("like: "+LikedPositions);
            }else{
                scale.setDuration(300);
                scale.setInterpolator(new OvershootInterpolator());
                BtnLike.startAnimation(scale);
                BtnLike.setBackgroundResource(R.drawable.like_pressed);
                LikedPositions.put(likeposition, true);
                System.out.println("like: "+LikedPositions);
            }
        }
    });

问题在这里,当我点击按钮时,切换工作,按键添加和删除! 但是当我滚动listview并查看回收,删除的密钥后退! 为什么呢?!

Ps:我将这些标签设置为喜欢和不喜欢按钮:

    holder.video_Like.setTag(R.string.Position, position);
    holder.video_Like.setTag(R.string.Video_ID, VIDEO_ID);

更新:我的自定义适配器getview:

    private final SparseBooleanArray LikedPositions     = new SparseBooleanArray();
    private final SparseBooleanArray DislikedPositions  = new SparseBooleanArray();

@Override
    public View getView(int position, View convertView, ViewGroup parent) {


        User_Session            = new SessionManagement(mActivity);

        View vi             = convertView;
        ViewHolder holder   = null;

        HashMap<String, String> mVideo  = new HashMap<String, String>();
        mVideo              = mData.get(position);

        VIDEO_URL           = mVideo.get("VIDEO_URL");
        VIDEO_ID            = mVideo.get("VIDEO_ID");
        LIKE_W_USER         = mVideo.get("LIKE_W_USER");
        DISLIKE_W_USER      = mVideo.get("DISLIKE_W_USER");     
        Like_with_user      = Boolean.parseBoolean(LIKE_W_USER);
        Dislike_with_user   = Boolean.parseBoolean(DISLIKE_W_USER);


        final String videoUid           = mVideo.get("uid");

        if(convertView == null) {
            vi      = mInflater.inflate(R.layout.layout_list_row, null);
            holder  = new ViewHolder();
            holder.video_Like           = (Button) vi.findViewById(R.id.video_Like);
            holder.video_Dislike        = (Button) vi.findViewById(R.id.video_disLike);
            vi.setTag(holder);

        }else{
            holder = (ViewHolder) vi.getTag();
        }

        holder.video_Like.setTag(R.string.Position, position);
        holder.video_Like.setTag(R.string.Video_ID, VIDEO_ID);
        holder.video_Dislike.setTag(R.string.Position, position);
        holder.video_Dislike.setTag(R.string.Video_ID, VIDEO_ID);

        if(User_Session.isLoggedIn() != true){


            holder.video_Comment.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    LoginWarn();
                }
            });

            holder.video_Like.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LoginWarn();
                }
            });
            holder.video_Dislike.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LoginWarn();
                }
            });

        }else{

            HashMap<String, String> user    = User_Session.GetUserDetails();
            final String User_TOKEN         = user.get(SessionManagement.KEY_USERTOKEN);

            if (Like_with_user == true){                                                    // Check that user liked this video before or not
                LikedPositions.put(position, true);
            }

            if (Dislike_with_user == true){                                                 // Check that user disliked this video before or not
                DislikedPositions.put(position, true);
            }

            if(LikedPositions.get(position)){                                               // Highlight Like Sign if user liked it before
                holder.video_Like.setBackgroundResource(R.drawable.like_pressed);
            }else{
                holder.video_Like.setBackgroundResource(R.drawable.like_normal);
            }

            if(DislikedPositions.get(position)){                                            // Highlight dislike Sign if user disliked it before
                holder.video_Dislike.setBackgroundResource(R.drawable.dislike_pressed);
            }else{
                holder.video_Dislike.setBackgroundResource(R.drawable.dislike_normal);
            }


            holder.video_Like.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int position            = (Integer) v.getTag(R.string.Position);
                    RelativeLayout layout   = (RelativeLayout) v.getParent();
                    Button BtnLike          = (Button) layout.getChildAt(0);
                    Button BtnDislike       = (Button) layout.getChildAt(1);

                    Ion.with(mActivity).load(Constants.Like_URL+v.getTag(R.string.Video_ID)+"/?token="+User_TOKEN).asString()
                    .setCallback(new FutureCallback<String>(){

                        @Override
                        public void onCompleted(Exception e, String result) {

                            if(e != null){
                                Toast.makeText(mActivity, "Sorry, Error Happened", Toast.LENGTH_SHORT).show();
                            }
                        }

                    });

                    if(DislikedPositions.get(position)){
                        BtnDislike.setBackgroundResource(R.drawable.dislike_normal);
                        DislikedPositions.delete(position);
                    }

                    if(LikedPositions.get(position)){
                        BtnLike.setBackgroundResource(R.drawable.like_normal);
                        LikedPositions.delete(position);
                    }else{
                        scale.setDuration(300);
                        scale.setInterpolator(new OvershootInterpolator());
                        BtnLike.startAnimation(scale);
                        BtnLike.setBackgroundResource(R.drawable.like_pressed);
                        LikedPositions.put(position, true);
                    }
                }
            });


            holder.video_Dislike.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int position            = (Integer) v.getTag(R.string.Position);
                    RelativeLayout layout   = (RelativeLayout) v.getParent();
                    Button BtnLike          = (Button) layout.getChildAt(0);
                    Button btnDislike       = (Button) layout.getChildAt(1);

                    Ion.with(mActivity).load(Constants.Dislike_URL+v.getTag(R.string.Video_ID)+"/?token="+User_TOKEN).asString()
                    .setCallback(new FutureCallback<String>(){

                        @Override
                        public void onCompleted(Exception e, String result) {

                            if(e != null){
                                Toast.makeText(mActivity, "Sorry, Error Happened", Toast.LENGTH_SHORT).show();
                            }
                        }

                    });

                    if(LikedPositions.get(position)){
                        BtnLike.setBackgroundResource(R.drawable.like_normal);
                        LikedPositions.delete(position);
                    }

                    if(DislikedPositions.get(position)){
                        btnDislike.setBackgroundResource(R.drawable.dislike_normal);
                        DislikedPositions.delete(position);
                    }else{
                        scale.setDuration(300);
                        scale.setInterpolator(new OvershootInterpolator());
                        btnDislike.startAnimation(scale);
                        btnDislike.setBackgroundResource(R.drawable.dislike_pressed);
                        DislikedPositions.put(position, true);
                    }
                }
            });

        }






        // we're near the end of the list adapter, so load more items
        if (position >= getCount() - 1){
            mSwipe.setRefreshing(true);
            loadmore();
        }




        return vi;
}

1 个答案:

答案 0 :(得分:0)

哇,getView()有很多事情要发生。蝙蝠,以下看起来真的错了。

 // we're near the end of the list adapter, so load more items
 if (position >= getCount() - 1){
     mSwipe.setRefreshing(true);
     loadmore();
 }

每个位置编号可以调用getView()方法3-4次。首次显示到屏幕时,可能会或可能不会调用所有项目。因此,确定何时将更多项目加载到适配器中非常糟糕。考虑到你说在你开始滚动之前不会出现问题......我想这可能是你的罪魁祸首。

您应该做的是将OnScollListener附加到ListView并根据适配器计数检查ListView.getLastVisiblePosition()以确定何时加载更多内容。

此外,在应用此修复程序之前,我只是删除该段代码,看看是否能解决您的问题。