Android之listview点击item当前item变色其他不变

-----------------转载请注明出处:http://blog.csdn.net/android_cll  

一:先看看效果图。。。。

Android之listview点击item当前item变色其他不变_第1张图片

二:实现步骤:

1.xml布局

    android:id="@+id/left_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f4f5f7"
    android:scrollbars="none">

2.activity里加载适配器完后添加这两句代码

listViewAdpret.setSelectedPosition(position);
listViewAdpret.notifyDataSetInvalidated();

3.适配器里面自定义一个变量

private int selectedPosition = 0;// 选中的位置
public void setSelectedPosition(int position) {
    selectedPosition = position;
}


4.判断是否选择当前item

if (selectedPosition == position) {
    itemlayoutb.setBackgroundColor(Color.parseColor("#ffffff"));
    textc.setTextColor(Color.parseColor("#ff0000"));
} else {
    itemlayoutb.setBackgroundColor(Color.TRANSPARENT);
    textc.setTextColor(Color.parseColor("#393939"));
}

代码不多,效果还是可以实现的,你们凑合看看吧。


你可能感兴趣的:(Android基础)