1. layout_search_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D5D5D5"
android:orientation="vertical" > <ImageView
android:id="@+id/search_listview_item_img"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
/> <TextView
android:id="@+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="@dimen/search_hint_size"
/> </LinearLayout>

2. 主布局插入listvie

<ListView
android:id="@+id/search_word_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

3. 主 .Java文件中

adapter = new SearchAdapter(this,R.layout.layout_search_list_item,
poiListList);
search_word_list.setAdapter(adapter); search_word_list.setOnItemClickListener(this);
    private  int last_item_position = -1;
private ImageView oldImageView;
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { ImageView imageView = (ImageView) view.findViewById(R.id.search_listview_item_img);
imageView.setBackgroundResource(R.drawable.image_floor_color);
        //android 获取其他layout,如果仅仅还设置点击项目的改变其背景图片,点击positon可能会有重复
//之前点过的还原
if (last_item_position!=-1&& last_item_position != position) {
oldImageView.setBackgroundResource(R.drawable.image_floor_gary);//把上次选中的样式去掉
}
oldImageView = imageView;
last_item_position = position;
}
public class SearchAdapter extends ArrayAdapter<GetResultFromPOIName.PoiList> {
private int resourceId;
private ImageView imageView ;
public SearchAdapter(Context context, int textViewResourceId,
List<GetResultFromPOIName.PoiList> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PoiList poiList = getItem(position);
View view; if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
            imageView = (ImageView) view.findViewById(R.id.search_listview_item_img);
if (last_item_position == position) {
//解决滑动listview的时候,选中的条目选中效果消失问题
// textView.setBackgroundColor(Color.BLUE);
imageView.setBackgroundResource(R.drawable.image_floor_color);
} else {
// textView.setBackgroundColor(Color.WHITE);
imageView.setBackgroundResource(R.drawable.image_floor_gary);
}
return view;
}

ListView 点击某一项换背景图片的更多相关文章

  1. uwp开发————换背景图片

    原文:uwp开发----换背景图片 用后台代码来实现对容器背景的切换,用本地图片作为背景. 把需要的图片素材放到Assets文件夹下 前台xaml代码如下: <Grid x:Name=" ...

  2. ppt怎么换背景图片|PPT换背景设置方法

    PPT怎么换背景?PPT背景可谓是PPT幻灯片的灵魂,优美绚丽的PPT背景能为自己做的PPT幻灯片锦上添花.今天,格子啦小编就教大家PPT换背景的方法,让你不再羡慕别人所做PPT的美丽背景,也可以自己 ...

  3. 初学ToggleButton 点击button,更换button背景图片;再次点击,恢复之前背景图

    上方的图标,R.drawable.register_checked  是选中图片 下方的图标,   R.drawable.register_unchecked 是未选中图片 默认是上方的选中效果.点击 ...

  4. android怎么换背景图片

    我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类 ...

  5. ListView点击或选中item改变背景

    点击或选中ListView中的一项后.使item背景改变,失去焦点相同显示选中的背景.又一次选中另外一项才刷新: 在Adapter中配置: public class MyAdapter extends ...

  6. ListView点击Item展开隐藏项(单项展开、多项展开、复杂布局时的展开处理)

    手机屏幕毕竟有限,当我们要显示较多数据时便不得不舍去一些次要信息.将主要信息优先显示,也使显示效果更加简洁美观.遇到类似的需求,我们使用最多的就是 ListView ,而假设每次点击一个 Item 都 ...

  7. Android中的ListView点击时的背景颜色设置

    想设置listview中每行在点击.选中等不同状态下有不同的背景颜色,或者背景图片. 这可以用Android的Selector来实现.它可以定义组件在不同状态下的显示方式. 新建一个xml文件list ...

  8. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  9. JS-定时器换背景

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

随机推荐

  1. 2436: [Noi2011]Noi嘉年华 - BZOJ

    Description NOI2011 在吉林大学开始啦!为了迎接来自全国各地最优秀的信息学选手,吉林大学决定举办两场盛大的 NOI 嘉年华活动,分在两个不同的地点举办.每个嘉年华可能包含很多个活动, ...

  2. ios 多任务学习笔记

    一.检测多任务是否支持: - (BOOL) isMultitaskingSupported{ BOOL result = NO; if ([[UIDevice currentDevice] respo ...

  3. Mongo:将查询结果转换为自定义类

    1.自定义类 public class MyClass { public string Name { get; set; } public int Corners { get; set; } } 2. ...

  4. Reactjs相比较原生方案是绝对的快吗?哪些情况下React有优势

    作者:尤雨溪链接:http://www.zhihu.com/question/31809713/answer/53544875来源:知乎著作权归作者所有,转载请联系作者获得授权.   1. 原生 DO ...

  5. 错误:没有为扩展名“.html”注册的生成提供程序。

    没有为扩展名“.html”注册的生成提供程序.可以在 machine.config 或 web.config 中的 <compilation><buildProviders> ...

  6. [转载]WebBrowser控件表单(form)的自动填写和提交

    话说有了WebBrowser类,终于不用自己手动封装SHDocVw的AxWebBrowser这个ActiveX控件了.这个类如果仅仅作为一个和IE一模一样浏览器,那就太没意思了(还不如直接用IE呢). ...

  7. PE文件结构详解(六)重定位

    前面两篇 PE文件结构详解(四)PE导入表 和 PE文件结构详解(五)延迟导入表 介绍了PE文件中比较常用的两种导入方式,不知道大家有没有注意到,在调用导入函数时系统生成的代码是像下面这样的: 在这里 ...

  8. php浮点数精确运算

    php浮点数精确运算 Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(strin ...

  9. [转载]Spring Java Based Configuration

    @Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...

  10. hdu 4686 Arc of Dream

    思路:构造矩阵 a[i]*b[i]=ax*bx*a[i-1]*b[i-1]+ax*by*a[i-1]+ay*bx*b[i-1]+ay*by 代码如下: #include<iostream> ...