ListView 點擊某一項換背景圖片,listview換背景圖片

來源:互聯網
上載者:User

ListView 點擊某一項換背景圖片,listview換背景圖片

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.