具有图像选择和取消选择的Horizo​​ntalListview

Har*_*hid 5 android android-widget horizontalscrollview android-2.2-froyo android-listview

select/unselect在listiview 中的图像中遇到问题.就我而言,

ByDefault->image color(Yellow)
First click->image color(Orange)
Second click->image color(Yellow)
Run Code Online (Sandbox Code Playgroud)

如果用户点击途中然后完美,但是当用户第一次点击第一张图片并第二次点击第二张图片时,两种图像颜色都是橙色(这是问题).

在我的情况下,一次只有一种图像颜色为橙色(表示已选中).

Nam*_*ung 1

  1. 如果您只支持 HoneyComb 及以上版本,那就很容易了。创建一个 StateListDrawable 并将其设置为列表视图项的背景。

选择器.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@drawable/item_focus" />
    <item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

列表视图项目的布局

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:padding="5dp" />
Run Code Online (Sandbox Code Playgroud)

最后,将列表视图选择模式设置为SINGLE

list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Run Code Online (Sandbox Code Playgroud)

2. 如果你设法支持pre HoneyComb,你将不得不编写自己的布局工具checkable。您这样做是为了使用选中状态进行锻炼。让我们以 LinearLayout 为例(你可以对其他布局做同样的事情)。

 package com.example.listviewactivestate;

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

 public class CustomLinearLayout extends LinearLayout implements Checkable {


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

private boolean checked = false;

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

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

@Override
public boolean isChecked() {
    return checked;
}

@Override
public void setChecked(boolean checked) {
    this.checked = checked;

    refreshDrawableState();

    // Propagate to childs
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(checked);
        }
    }
}

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    if (isChecked()) {
        mergeDrawableStates(drawableState, CHECKED_STATE_SET);
    }
    return drawableState;
}

@Override
public void toggle() {
    this.checked = !this.checked;
}
 }
Run Code Online (Sandbox Code Playgroud)

在 xml 中使用此自定义视图

 <?xml version="1.0" encoding="utf-8"?>
 <com.example.listviewactivestate.CustomLinearLayout 

 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/selector"
>

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:padding="5dp" />

</com.example.listviewactivestate.CustomLinearLayout >
Run Code Online (Sandbox Code Playgroud)

改成state_activatedstate_checked

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:drawable="@drawable/item_focus" />
   <item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

还将列表视图选择模式设置为SINGLE。如果不行的话就onItemClickEvent这样添加

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                list.setItemChecked(position, true);//make sure click item is set to checked.

            }
        });
Run Code Online (Sandbox Code Playgroud)