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

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

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

问题是这个方法只有在 itrem 的新检查状态为 CHECKED 时才会执行。如果我取消选中它,它不会执行。

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

有什么线索吗?

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

主要 Activity :

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

最佳答案

像这样尝试

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

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

这是一个单选 ListView 。 需要改,需要改成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

步骤1)

/**
 * 
 */
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));
            }
        }
    }
}

第二步)

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" />

希望对你有所帮助。

关于android - MultiChoiceModeListener.onItemCheckedStateChanged() 仅在选中项目时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581897/

相关文章:

android - FragmentPagerAdapter 不适用于 fragment 的娱乐

java - 使用 viewPager 滚动所有布局

java - 删除目录时显示问题

java - ndk中的原生调用错误

android - 单个listview项目android的大小

android - 如何将ListView滚动到底部?

android - Facebook 安卓 SDK 4.0 : newGraphPathRequest returns "An active access token must be used to query information about the current user."

android - IBM Worklight - 应用程序在不同的 Android 操作系统版本中显示和行为不同

java - 如何在android中以编程方式设置listview高度

安卓 : How to use mFusedLocationClient callback (resulting location) in current mainthread