无法选择ListViewItem

时间:2012-07-06 00:46:16

标签: android listview

我的列表视图中有一个可检查的项目

public class CheckableMessageOut extends RelativeLayout implements Checkable{
    public CheckableMessageOut(Context context) {
        super(context);
    }

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

    LinearLayout wrapper;
    private boolean checked = false;

    @Override
    public void setChecked(boolean b) {
        if (checked!=b){
            if (b){
                wrapper.setBackgroundResource(R.drawable.label_msg_out_selected);
            } else {
                wrapper.setBackgroundResource(R.drawable.label_msg_out);
            }
        }
        checked = b;
    }

我像这样使用它

<ru.kurganec.vk.messenger.ui.helper.CheckableMessageOut xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

        >
    <LinearLayout
            android:id="@+id/wrapper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/label_msg_out"
            android:orientation="vertical">
        <TextView
                android:id="@+id/text_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="ad"

                />
        <LinearLayout
                android:id="@+id/list_attach"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:orientation="vertical">
        </LinearLayout>
    </LinearLayout>

    <TextView
            android:id="@+id/text_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/wrapper"

            android:text="06.06"/>

</ru.kurganec.

vk.messenger.ui.helper.CheckableMessageOut&GT;

有时我会以编程方式向@ id / list_attach添加一些视图,然后发生一些故事。我无法选择附加视图的项目。

整个它看起来像这样。

Message with photo cannot be selected but others(without photos) can be selected

通过说选择我的意思是检查。 我将此字段设置为mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

为什么我不能选择项目?


更新

片段类http://pastebin.com/BZRhahbi这是我的应用程序的主要部分,所以它看起来像“上帝级”反模式

适配器类http://pastebin.com/Av7ntUmG

适配器的项目类http://pastebin.com/MdYc7Ksw

3 个答案:

答案 0 :(得分:0)

我认为您的问题可能就在这里:

@Override
    public void setChecked(boolean b) {
        if (checked!=b){
            if (b){
                wrapper.setBackgroundResource(R.drawable.label_msg_out_selected);
            } else {
                wrapper.setBackgroundResource(R.drawable.label_msg_out);
            }
        }
        checked = b;
    }

尝试:

@Override
    public void setChecked(boolean b) {
        if (checked!=b){
                wrapper.setBackgroundResource(R.drawable.label_msg_out_selected);
              else {
                wrapper.setBackgroundResource(R.drawable.label_msg_out);
            }
        }
        checked = b;
    }

if(b)是不必要的代码。

答案 1 :(得分:0)

我认为实现此行为的更好方法是使用StateListDrawable。这样,系统将在状态变化期间负责切换抽屉。 您可以通过XML定义这样的drawable。

例如,res / drawable / item_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/item_bg_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/item_bg_normal" android:state_checked="false"/>

</selector>

并将其设置为可检查布局的背景资源:

<ru.kurganec.vk.messenger.ui.helper.CheckableMessageOut xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_bg">

并且您的可检查布局代码应该是这样的:

public class CheckableMessageOut extends RelativeLayout implements Checkable{
public CheckableMessageOut(Context context) {
    super(context);
}

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

LinearLayout wrapper;
private boolean checked = false;

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    if (checked) {
        final int[] states = super.onCreateDrawableState(extraSpace + 1);
        mergeDrawableStates(states,
                new int[] { android.R.attr.state_checked });
        return states;
    }
    return super.onCreateDrawableState(extraSpace);
}

@Override
public void setChecked(boolean c) {
    if (checked != c) {
        checked = c;
        refreshDrawableState();
    }
}

确保覆盖 onCreateDrawableState(int)方法,并在需要时将状态android.R.attr.state_checked添加到可绘制状态数组,并调用 refreshDrawableState()当检查状态改变时。

如果有任何问题,请查看此article

答案 2 :(得分:0)

我解决了问题

如果您在ListView中有可点击的视图

,只需将其添加到根视图即可
 android:descendantFocusability="blocksDescendants"
相关问题