item点击变色

onItemClick理解和点击item后背景变色的实现

博客分类:  

android

 

我们有时做一个菜单,点击后,背景变为点击后色,效果如下:



 

 

这里只是简单介绍实现过程:

定义存放菜单的listview:

Xml代码  收藏代码

  1. <ListView  

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

  3.         android:layout_width="match_parent"  

  4.         android:layout_height="match_parent"  

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

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

 

 


转载于:https://my.oschina.net/u/2531348/blog/609050

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值