gpt4 book ai didi

安卓 ListView : Can't get reliable checking behavior for CHOICE_MODE_SINGLE

转载 作者:太空狗 更新时间:2023-10-29 15:18:59 24 4
gpt4 key购买 nike

这里是我所说的怪异的意思:

  1. 通常需要按 2 次才能正确更改项目的背景颜色。
  2. 当列表被更改时,Android 似乎没有正确地使 ListView 无效,因为被检查的重新使用的 View 在新项目上有被检查的背景(我已经通过调试器确认不是 选中)。
  3. 多个项目看起来像是被选中,事实上,当我再次点击它们时“正确地”取消选中,除了 ListView 仅报告最近选中的项目.
  4. 我第一次选中某个项目时,无法通过单击取消选中它。
  5. 如果我将选择模式更改为 ListView.CHOICE_MODE_MULTIPLE,事情或多或少会正常工作,当然,我不希望进行多项选择。

我正在使用自定义适配器和自定义布局。哦,还有,现在目标是 4.0.3。这是列表的代码:

ListView categoryList = (ListView) findViewById(R.id.categoryList);
categoryList.setAdapter(categoryAdapter);
categoryList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
categoryList.setItemsCanFocus(false);
categoryList.setOnItemClickListener(categoryAdapter);

这是点击监听器:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
ListView listView = (ListView) parent;
RemoteListItem remoteListItem = (RemoteListItem) view.getTag();

if (remoteListItem.isEnabled()) {
remoteListItem.action(view);
}
view.invalidate(); /added out of sheer desperation
}

这是我正在使用的 RelativeLayout 的扩展版本:

package com.sastraxi.machineshop.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Checkable;
import android.widget.RelativeLayout;

/**
* RelativeLayout that implements the Checkable interface.
* Set this view's tag as a Checkable, and this layout will delegate
* Checkable's interface methods to the tag object.
*/
public class CheckableRelativeLayout extends RelativeLayout implements Checkable {

@Override
public boolean isClickable() {
return false;
}

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

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

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

/**
* Delegates to (Checkable) getTag().
*/
public boolean isChecked() {
try {
Checkable checkableTag = (Checkable) getTag();
return checkableTag.isChecked();
} catch (ClassCastException e) {
Log.w("CheckableRelativeLayout", "Tag is not an instance of Checkable; this object won't do anything useful.");
} catch (NullPointerException e) {
Log.w("CheckableRelativeLayout", "Tag is null; this object won't do anything useful.");
}
return false;
}

/**
* Delegates to (Checkable) getTag().
*/
public void setChecked(boolean checked) {
try {
Checkable checkableTag = (Checkable) getTag();
checkableTag.setChecked(checked);
invalidate();
} catch (ClassCastException e) {
Log.w("CheckableRelativeLayout", "Tag is not an instance of Checkable; this object won't do anything useful.");
} catch (NullPointerException e) {
Log.w("CheckableRelativeLayout", "Tag is null; this object won't do anything useful.");
}
}

/**
* Delegates to (Checkable) getTag().
*/
public void toggle() {
try {
Checkable checkableTag = (Checkable) getTag();
checkableTag.toggle();
invalidate();
} catch (ClassCastException e) {
Log.w("CheckableRelativeLayout", "Tag is not an instance of Checkable; this object won't do anything useful.");
} catch (NullPointerException e) {
Log.w("CheckableRelativeLayout", "Tag is null; this object won't do anything useful.");
}
}

private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked
};

/**
* Reflect the delegate Checkable's state in this View's state set.
*/
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}

}

这是它代理的列表项类型:

public abstract class RemoteListItem implements Checkable {

private final String name;
private final String extra;
private boolean enabled = true;
private boolean selected = false;

public boolean isChecked() {
return selected;
}

public void toggle() {
selected = !selected;
}

public void setChecked(boolean checked) {
selected = checked;
}

public RemoteListItem(String name, String extra) {
this.name = name;
this.extra = extra;
}

public String getExtra() {
return extra;
}

public String getName() {
return name;
}

public abstract void action(View viewInList);

public boolean isEnabled() {
return enabled;
}

public void setEnabled(Boolean enabled) {
this.enabled = enabled;
RemoteListAdapter.super.notifyDataSetChanged();
}

public boolean isSelectable() {
return true;
}

}

这是为项目展开的布局:

<?xml version="1.0" encoding="utf-8"?>
<com.sastraxi.machineshop.ui.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:padding="12dp"
android:gravity="center_vertical"
android:background="@drawable/listitem_background">

<TextView
android:id="@+id/key"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentLeft="true"
android:inputType="none"
/>

<TextView
android:id="@+id/value"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/faded_text_colour"
android:layout_alignParentRight="true"
android:inputType="none"
/>

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"
android:layout_marginTop="5dip"
android:layout_marginRight="2dip"
android:gravity="right"
android:visibility="gone"
android:layout_alignParentRight="true"/>

</com.sastraxi.machineshop.ui.CheckableRelativeLayout>

还有,@drawable/listitem_background是一个状态列表,勾选的背景色就是从这里来的。对于为什么事情没有按照我期望的方式进行,我感到非常迷茫。好像我在某处遗漏了一个 view.invalidate(),但我无法理解在哪里。

最佳答案

听起来你的第一次触摸是给它焦点,第二次是点击。将项目设置为不可聚焦,您的第一次触摸应注册为点击。

关于安卓 ListView : Can't get reliable checking behavior for CHOICE_MODE_SINGLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9832012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com