item點擊變色

onItemClick理解和點擊item後背景變色的實現

 

咱們有時作一個菜單,點擊後,背景變爲點擊後色,效果以下:android



 

 

這裏只是簡單介紹實現過程:app

定義存放菜單的listview:ide

Xml代碼  收藏代碼字體

  1. <ListView  this

  2.         android:id="@+id/subject_menu_category_lv"  spa

  3.         android:layout_width="match_parent"  orm

  4.         android:layout_height="match_parent"  xml

  5.         android:layout_above="@id/subject_menu_bottom_layout"  blog

  6.         android:layout_below="@id/subject_menu_top"  

  7.         android:listSelector="@drawable/selector_subject_menu_listview"  

  8.         android:divider="@color/menu_divider"  

  9.         android:dividerHeight="1dp"  

  10.         android:background="@color/menu_item_normal"  

  11.         >  

  12.     </ListView>  

 

這裏將devider設置爲1dp,指定顏色就有分隔線了,若是不想要分隔線或將分隔線放在item裏寫,listview就使用android:divider="@null"。

顏色配置以下:

Java代碼  收藏代碼

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <resources>  

  3.   

  4.   

  5.       

  6.     <!-- 學科分類顏色設置 -->  

  7.     <color name="menu_item_normal">#8f8c87</color>  

  8.     <color name="menu_item_press">#6b6863</color>  

  9.     <color name="menu_text_normal">#dddddd</color>  

  10.     <color name="menu_text_press">#fefefe</color>  

  11.     <color name="menu_text_normal">#dddddd</color>  

  12.     <color name="menu_divider">#bab7b1</color>  

  13.       

  14.       

  15.       

  16.       

  17. </resources>  

 

 

listview的adapter:

Java代碼  收藏代碼

  1. package com.yiduoyun.tiku.adapter;  

  2.   

  3. import java.util.ArrayList;  

  4.   

  5. import android.app.Activity;  

  6. import android.content.Context;  

  7. import android.view.View;  

  8. import android.view.ViewGroup;  

  9. import android.widget.TextView;  

  10.   

  11. import com.yiduoyun.tiku.R;  

  12. import com.yiduoyun.tiku.model.SubjectCatalog;  

  13.   

  14. /** 

  15.  * 學科的適配器 

  16.  */  

  17. public class SubjectCatalogAdapter extends ArrayListAdapter<SubjectCatalog> {  

  18.       

  19.     private Context context = null;  

  20.       

  21.     /** 

  22.      * 首次使用,默認第一項目背景變黑 

  23.      */  

  24.     private boolean first = true;  

  25.       

  26.     public SubjectCatalogAdapter(Context context) {  

  27.         super(context);  

  28.         this.context = context;  

  29.     }  

  30.   

  31.   

  32.     public void setSubjectCatalogList(ArrayList<SubjectCatalog> subjectCatalogList) {  

  33.         setList(subjectCatalogList);  

  34.     }  

  35.   

  36.   

  37.     @Override  

  38.     public View getView(int position, View convertView, ViewGroup parent) {  

  39.           

  40.         ViewHolder viewHolder = null;  

  41.           

  42.         if (convertView == null){  

  43.             convertView = ((Activity)getContext()).getLayoutInflater().inflate(  

  44.                     R.layout.subject_category_item, null);  

  45.   

  46.             viewHolder = new ViewHolder();  

  47.               

  48.             viewHolder.subjectName = (TextView) convertView.findViewById(R.id.category_name_tv);  

  49.               

  50.             convertView.setTag(viewHolder);  

  51.         }  

  52.         else {  

  53.             viewHolder = (ViewHolder) convertView.getTag();  

  54.         }  

  55.           

  56.         /** 

  57.          * 將第一個item設置爲選中狀態 

  58.          */  

  59.         if(first == true && position == 0){  

  60.             convertView.setBackgroundResource(R.color.menu_item_press);//背景變黑色  

  61.             viewHolder.subjectName.setTextColor(context.getResources().getColor(R.color.menu_text_press));//字體變白色  

  62.             first = false;  

  63.         }  

  64.           

  65.           

  66.         SubjectCatalog sc = getItem(position);  

  67.         viewHolder.subjectName.setText(sc.getName());  

  68.           

  69.         return convertView;  

  70.       

  71.     }  

  72.       

  73.     class ViewHolder{  

  74.         TextView subjectName;//學科名  

  75.     }  

  76. }  

 

每個item是那個的簡單:

subject_category_item.xml

Xml代碼  收藏代碼

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical"  

  6.     android:padding="8dp" >  

  7.       

  8.     <TextView   

  9.         android:id="@+id/category_name_tv"  

  10.         android:layout_width="match_parent"  

  11.         android:layout_height="wrap_content"  

  12.         android:gravity="center"  

  13.         android:text="語文"  

  14.         android:textColor="@color/menu_text_normal"  

  15.         android:textSize="20sp"/>  

  16.   

  17. </LinearLayout>  

 

 

 

重點點擊事件,背景變色功能:

Java代碼  收藏代碼

  1. categoryListView = (ListView) getActivity().findViewById(R.id.subject_menu_category_lv);  

  2.           

  3.   

  4.         adapter = new SubjectCatalogAdapter(getActivity());  

  5.         adapter.setSubjectCatalogList(subjectCatalogList);  

  6.         categoryListView.setAdapter(adapter);  

  7.   

  8.           

  9.           

  10.           

  11.         categoryListView.setOnItemClickListener(new OnItemClickListener() {  

  12.   

  13.             @Override  

  14.             public void onItemClick(AdapterView<?> parent, View view,//這裏的parent是listview,由於setOnItemClickListener能夠是listview或gridview等,系統定爲泛型,運行時系統會傳入的  

  15.                     int position, long id) {  

  16.                   

  17.                 /** 

  18.                  * 設置點擊效果背景 

  19.                  */  

  20.                 for(int i=0;i<parent.getCount();i++){  

  21.                       

  22.                     /** 

  23.                      * 由於parent這裏是listview,因此parent.getChildAt(i)就是一個一個的item 

  24.                      */  

  25.                     View item=parent.getChildAt(i);  

  26.                       

  27.                     /** 

  28.                      * 找到item裏的每個元素再進行相關操做 

  29.                      */  

  30.                     TextView categoryNameTextView = (TextView)(item.findViewById(R.id.category_name_tv));  

  31.                       

  32.                       

  33.                     if (position == i) {  

  34.                         item.setBackgroundResource(R.color.menu_item_press);  

  35.                         categoryNameTextView.setTextColor(getResources().getColor(R.color.menu_text_press));  

  36.                     } else {  

  37.                         item.setBackgroundResource(R.color.menu_item_normal);  

  38.                         categoryNameTextView.setTextColor(getResources().getColor(R.color.menu_text_normal));  

  39.                     }  

  40.                 }  

  41.                   

  42.                   

  43.                 /** 

  44.                  * 界面切換 

  45.                  */  

  46.                 Bundle bundle = new Bundle();  

  47.                 SubjectCatalog sc = subjectCatalogList.get(position);  

  48.                 bundle.putSerializable(Constant.TAG_SUBJECT_CATALOG , sc);  

  49.                 MainSlidingMenuActivity.getInstance().startToActivity(new SubjectHomeFragment() , bundle);  

  50.             }  

  51.         });  

 

 

點擊事件最重要理解onItemClick的參數:

AdapterView<?> arg0 參數得意思:官方解釋說:the AdapterView where the click happened. 也就是當前點擊的adapterview,這個參數是系統自動傳入的,咱們也不用調用,通常經常使用第二個和第三個參數。

 

而後給你講AdapterView<?> ,這個屬於java基礎的內容,叫作泛型,就是告訴你傳入的參數是哪一種類型。 好比String<?>,List<T>,Map<K,V>,String<E> ?表示不肯定的java類型。 T 表示java類型。 K V 分別表明java鍵值中的Key Value。 E 表明Element。 ListView, GridView, Spinner and Gallery 中都要用到adapter,因此這裏用問好表示不肯定傳入的是哪一種類型,不用咱們關係,系統自動傳入,到時使用時就是什麼(ListView, GridView, Spinner and Gallery中的一個)。

 

這個方法的參數是這樣的AdapterView<?> parent, View view,第一個是指父View,好比你的是ListView,那麼arg0就是ListView了,arg1就是你點擊的那個Item的View。arg2是position,arg3是id,相對於我上面舉的ListView的例子來講,position是你適配器裏面的position,通常是第幾個項,id是哪一個項View的id。當ADT的版本高一些,自動導入接口的必須實現的方法的時候,對於以上的方法會返回public void onItemClick(AdapterView<?> parent, View view, int position, long id

 

 

上面代碼先找到listview的item,再從item裏找出元素,再進行點擊效果操做,重點是下面兩句代碼:

Java代碼  收藏代碼

  1. /** 

  2.                      * 由於parent這裏是listview,因此parent.getChildAt(i)就是一個一個的item 

  3.                      */  

  4.                     View item=parent.getChildAt(i);  

  5.                       

  6.                     /** 

  7.                      * 找到item裏的每個元素再進行相關操做 

  8.                      */  

  9.                     TextView categoryNameTextView = (TextView)(item.findViewById(R.id.category_name_tv));  

相關文章
相關標籤/搜索