ListView 点击某一项换背景图片

Screenshot_2015-12-11-16-36-19

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;
        }

转载于:https://www.cnblogs.com/520-1314/p/5039578.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值