3

問題が発生しています。ListActivity拡張して実装する私のメインアクティビティMultiChoiceModeListenerには、オーバーライドされたメソッドがありますonItemCheckedStateChanged()

問題は、このメソッドが実行されるのは、itremの新しいチェック状態がチェックされた場合のみであるということです。チェックを外すと実行されません。

私は自分のからプログラムでアイテムをチェックします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;
    }
}
4

2 に答える 2

2

このようにしてみてください

私はあなたの問題を解決するカスタム多肢選択リストビューを使用しました。完全なソースコードについては、以下のリンクを参照してください。

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:-

ここに画像の説明を入力してください

手順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));
            }
        }
    }
}

ステップ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;
    }
}

ステップ3)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" />

これがあなたの助けになることを願っています。

于 2013-03-23T02:17:05.383 に答える
0

それで、私は私が持っていた同様の問題を解決しました。

考慮すべきいくつかの事柄:

  1. BaseAdapterにはデフォルトで安定したIDがありませんが、選択/選択解除が正しく機能するには、アダプターに安定したIDが必要です。AbsListView.javaのソースコードをチェックして確認してください。したがって、hasStableIds()アダプタ実装のメソッドをオーバーライドしてtrueをgetItemId(int position)返し、各行に一意のIDを返すようにしてください。

  2. この2つ目が本当に必要かどうかはわかりませんが、念のため...デフォルトでは、リスト内の実装行は選択できません。アイテムに指を置いている間だけ、ハイライトが表示されます。ある種の静的ハイライトを表示し、行を選択したままにしたい場合は、アイテムにCheckableインターフェイスを実装する必要があります。CheckableLinearLayout私の場合、すべてのアイテムのベースとしてカスタム実装を使用しました。それはうまくいった。インターネットで実装を見つけるか、私に返信してください。ここに投稿します。

そして一般的に、私はソースコードを調べることをお勧めしAbsListViewます、それは何がうまくいかなかったかを理解するのに本当に役立ちます

于 2014-01-07T13:39:47.843 に答える