星空网 > 软件开发 > 操作系统

ListView 点击某一项换背景图片

ListView 点击某一项换背景图片images/loading.gif' data-original="http://images2015.cnblogs.com/blog/771964/201512/771964-20151211165500933-1676635592.jpg" width="172" height="303">

1. layout_search_list_item.

<LinearLayout ="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 点击某一项换背景图片

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

突发!油罐爆炸导致多个亚马逊仓库被迫关闭 :https://www.goluckyvip.com/news/7781.html
天猫淘宝推出“跨境供货平台”;Shopee多个国家佣金等费率上涨 :https://www.goluckyvip.com/news/7782.html
lazada的新手卖家注意事项有哪些? :https://www.goluckyvip.com/news/7783.html
重要提醒!英国卖家从9月30日起,没有CDS账号将影响递延清关!:https://www.goluckyvip.com/news/7784.html
Shopee做什么类目比较好卖?:https://www.goluckyvip.com/news/7785.html
沃尔玛物流有大动作了!中国卖家占领沃尔玛:https://www.goluckyvip.com/news/7786.html
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流