MultiChoiceModeListener.onItemCheckedStateChanged()仅在选中项目时执行

时间:2013-03-23 00:25:06

标签: android android-listview android-adapter android-checkbox

我遇到了问题。 在我的主要活动(扩展ListActivity和实施MultiChoiceModeListener)中,我有覆盖方法onItemCheckedStateChanged()

问题是此方法仅在itrem的新检查状态为CHECKED时执行。如果我取消选中它,它就不会执行。

我从DataListAdapter以编程方式检查项目。我的项目布局包含CheckBox,当它被选中时,我使用控件的onCheckedChanged()来更改列表项检查状态。

任何线索?

这是我的代码(仅限相关代码):

主要活动:

public class MainActivity extends ListActivity implements MultiChoiceModeListener
{
    @Override
    protected void onCreate (Bundle bundle)
    {
        super.onCreate (bundle);
        // Set our view from the "main" layout resource
        setContentView (R.layout.main);

        this.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        this.getListView().setMultiChoiceModeListener(this);

        _dataAdapter = new ServerListAdapter (this);
        this.getListView(). setAdapter(_dataAdapter);
        registerForContextMenu (this.getListView());
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean isChecked) 
    {
        // Some code that only is executed when the setItemChecked is true on the DataListAdapter.        
    }
}

列表适配器

public class ServerListAdapter extends BaseAdapter 
{
    @Override
    public View getView (final int position, View convertView, ViewGroup parent)
    {
        View view = (convertView == null ?
                _context.getLayoutInflater().inflate(
                R.layout.server_list_item_view,
                parent, 
                false) : convertView);

        final ListView listView = (ListView)parent;
        ((CheckBox)view.findViewById(R.id.chkItemServerSelected)).setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton pCompound, boolean arg1) 
            {
                listView.setItemChecked(position, pCompound.isChecked());
            }       
        });

        return view;
    }
}

2 个答案:

答案 0 :(得分:2)

尝试这样

我已经完成了自定义多项选择ListView,可以解决您的问题。 通过以下链接获取补充源代码。

<强> http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

这是一个单选列表视图。 您需要更改并需要更改为multipleChoice

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:choiceMode="multipleChoice"
        android:listSelector="#00000000" />

Custom Multiple Choice ListView:-

enter image description here

Steps1)

/**
 * 
 */
package com.custom.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.RelativeLayout;


public class CheckableRelativeLayout extends RelativeLayout implements
        Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

    public CheckableRelativeLayout(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public CheckableRelativeLayout(Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    /*
     * @see android.widget.Checkable#isChecked()
     */
    public boolean isChecked() {
        return isChecked;
    }

    /*
     * @see android.widget.Checkable#setChecked(boolean)
     */
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        for (Checkable c : checkableViews) {
            c.setChecked(isChecked);
        }
    }

    /*
     * @see android.widget.Checkable#toggle()
     */
    public void toggle() {
        this.isChecked = !this.isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Read the custom XML attributes
     */
    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }

        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}

步骤2)

P

ackage com.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.CheckBox;
public class InertCheckBox extends CheckBox {

    public InertCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public InertCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InertCheckBox(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        return false;
    }
}

Step3)listitem.xml

<com.custom.view.InertCheckBox
    android:id="@+id/multiitemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp"
    android:focusable="false" />

<TextView
    android:id="@+id/singleitemId"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/multiitemCheckBox"
    android:focusable="false"
    android:textSize="14sp" />

希望对你有所帮助。

答案 1 :(得分:0)

所以,我解决了类似的问题。

需要考虑的一些事项:

  1. 默认情况下,BaseAdapter没有稳定的ID,而为了使select / deselect正常工作,你的适配器应具有稳定的id。检查AbsListView.java源代码以确认。因此,在适配器实现中覆盖hasStableIds()方法以返回true,并确保getItemId(int position)为每行返回唯一ID。

  2. 不确定第二件事是否真的有必要,但以防万一...默认情况下,列表中的实现行是不可选的 - 只有当您按住项目时,它们才会显示一些突出显示。如果你想显示某种静态高亮显示并保持选择行 - 你的项目应该实现Checkable界面。至于我 - 我使用自定义CheckableLinearLayout实现作为我所有项目的基础。那很有效。您可以在互联网上找到实现或回复给我,我会在这里发布一个。

  3. 一般情况下,我建议查看AbsListView源代码,它真的有助于理解出错的地方

相关问题