5

我在图像select/unselect listiview中出现问题。 在我的情况,Horizo​​ntalListview与图像选择和取消选择

ByDefault->image color(Yellow) 
First click->image color(Orange) 
Second click->image color(Yellow) 

如果用户点击过的方式那就完美了,但是当第一图像和第二次点击第二图像在用户第一次点击那么这两个图像颜色为橙色(这是问题)。

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

+0

请添加一些代码或图片。对我来说很难理解。 –

+0

@Yul看到这个http://pastebin.com/3eeZ38dN – Harshid

回答

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

selector.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> 

列表视图项目的布局

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

最后,设置你的列表视图中选择模式SINGLE

list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

2.如果你能支持蜂窝前,你必须写自己的布局实现可检验的。你这样做是为了锻炼使用检查状态。以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; 
} 
} 

在使用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 > 

变化state_activated这个自定义视图state_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> 

还设置列表视图选择模式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. 

      } 
     }); 
+0

谢谢。但在我的布局我有2 textview和imageview.So如何使颜色只有imageview,我的textview也可点击。 – Harshid

+0

我认为你可以在另一个布局中包裹布局并添加更多2按钮。 –

+0

雅我有主要的线性布局,然后所有对象与相关的布局。 – Harshid