在列表视图中切换时滚动时自动获取检查android [英] Switches in listview automatically gets check android when scrolled

查看:73
本文介绍了在列表视图中切换时滚动时自动获取检查android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码片段出现在getView()中.

Below there is the snipset of my code present in getView() .

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

    final ViewHolder holder;

    final InterestItems temp;
    temp = getItem(position);
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(layoutResource, null, false);

        holder = new ViewHolder();
        holder.tv_interest_name = (TextView) convertView.findViewById(R.id.tv_interestName_custom_row);
        holder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.switch_custom_row);

        convertView.setTag(holder);
        if(temp.isInterested)
        {
            holder.switchCompat.setChecked(true);
        }

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


    holder.tv_interest_name.setText(temp.getInterestName());



    holder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String name = temp.getInterestName();
            if (isChecked) {
                if (interfaceObject != null) {
                    interfaceObject.addedInterest(name, position);
                    list.get(position).setIsInterested(true);
                }
            } else {
                if (interfaceObject != null) {

                    interfaceObject.removedInterest(name, position);
                    list.get(position).setIsInterested(false);

                }
            }
        }
    });

    return convertView;
}

当我自动滚动时,将检查某些开关,但没有将任何值插入到数组中,也未在特定点命中调试器.我不明白为什么会这样.您的任何帮助将不胜感激.

When i scroll automatically some of the switches get checked but none of the value is inserted into the array nor the debugger is hit at a specific point . I can't understand why it is happening . Any help from your side will be appreciated .

推荐答案

使用以下代码:

listView.setOnScrollListener(lv_onScrollListener);

private OnScrollListener lv_onScrollListener = new OnScrollListener() {
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            adapter.setScroll(false);
            adapter.notifyDataSetChanged();
        } else{
            adapter.setScroll(true);
        }
    }
};

在适配器类中:

Boolean isScroll = false;

public void setScroll(Boolean status) {
    isScroll = status;
}
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        if (!isScroll)
        {
        //Do it here
        }
    }
});

这篇关于在列表视图中切换时滚动时自动获取检查android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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