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

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



 

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

定义存放菜单的listview:

<ListView
        android:id="@+id/subject_menu_category_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/subject_menu_bottom_layout"
        android:layout_below="@id/subject_menu_top"
        android:listSelector="@drawable/selector_subject_menu_listview"
		android:divider="@color/menu_divider"
		android:dividerHeight="1dp"
		android:background="@color/menu_item_normal"
		>
    </ListView>

这里将devider设置为1dp,指定颜色就有分隔线了,如果不想要分隔线或将分隔线放在item里写,listview就使用android:divider="@null"。

颜色配置如下:

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


    
    <!-- 学科分类颜色设置 -->
    <color name="menu_item_normal">#8f8c87</color>
    <color name="menu_item_press">#6b6863</color>
    <color name="menu_text_normal">#dddddd</color>
    <color name="menu_text_press">#fefefe</color>
    <color name="menu_text_normal">#dddddd</color>
    <color name="menu_divider">#bab7b1</color>
    
    
    
    
</resources>

 

listview的adapter:

package com.yiduoyun.tiku.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

/**
 * 学科的适配器
 */
public class SubjectCatalogAdapter extends ArrayListAdapter<SubjectCatalog> {
	
	private Context context = null;
	
	/**
	 * 首次使用,默认第一项目背景变黑
	 */
	private boolean first = true;
	
	public SubjectCatalogAdapter(Context context) {
		super(context);
		this.context = context;
	}


	public void setSubjectCatalogList(ArrayList<SubjectCatalog> subjectCatalogList) {
		setList(subjectCatalogList);
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewHolder = null;
		
		if (convertView == null){
			convertView = ((Activity)getContext()).getLayoutInflater().inflate(
					R.layout.subject_category_item, null);

			viewHolder = new ViewHolder();
			
			viewHolder.subjectName = (TextView) convertView.findViewById(R.id.category_name_tv);
			
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		/**
		 * 将第一个item设置为选中状态
		 */
		if(first == true && position == 0){
			convertView.setBackgroundResource(R.color.menu_item_press);//背景变黑色
			viewHolder.subjectName.setTextColor(context.getResources().getColor(R.color.menu_text_press));//字体变白色
			first = false;
		}
		
		
		SubjectCatalog sc = getItem(position);
		viewHolder.subjectName.setText(sc.getName());
		
		return convertView;
	
	}
	
	class ViewHolder{
		TextView subjectName;//学科名
	}
}

 

每一个item是那个的简单:

subject_category_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp" >
    
    <TextView 
        android:id="@+id/category_name_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="语文"
        android:textColor="@color/menu_text_normal"
        android:textSize="20sp"/>

</LinearLayout>

重点点击事件,背景变色功能:

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

		adapter = new SubjectCatalogAdapter(getActivity());
		adapter.setSubjectCatalogList(subjectCatalogList);
		categoryListView.setAdapter(adapter);

		
		
		
		categoryListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,//这里的parent是listview,因为setOnItemClickListener可以是listview或gridview等,系统定为泛型,运行时系统会传入的
					int position, long id) {
				
				/**
				 * 设置点击效果背景
				 */
				for(int i=0;i<parent.getCount();i++){
					
					/**
					 * 因为parent这里是listview,所以parent.getChildAt(i)就是一个一个的item
					 */
		            View item=parent.getChildAt(i);
		            
		            /**
		             * 找到item里的每一个元素再进行相关操作
		             */
		            TextView categoryNameTextView = (TextView)(item.findViewById(R.id.category_name_tv));
					
		            
		            if (position == i) {
		            	item.setBackgroundResource(R.color.menu_item_press);
		                categoryNameTextView.setTextColor(getResources().getColor(R.color.menu_text_press));
		            } else {
		            	item.setBackgroundResource(R.color.menu_item_normal);
		                categoryNameTextView.setTextColor(getResources().getColor(R.color.menu_text_normal));
		            }
		        }
				
				
				/**
				 * 界面切换
				 */
				Bundle bundle = new Bundle();
				SubjectCatalog sc = subjectCatalogList.get(position);
				bundle.putSerializable(Constant.TAG_SUBJECT_CATALOG , sc);
				MainSlidingMenuActivity.getInstance().startToActivity(new SubjectHomeFragment() , bundle);
			}
		});

点击事件最重要理解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里找出元素,再进行点击效果操作,重点是下面两句代码:

/**
					 * 因为parent这里是listview,所以parent.getChildAt(i)就是一个一个的item
					 */
		            View item=parent.getChildAt(i);
		            
		            /**
		             * 找到item里的每一个元素再进行相关操作
		             */
		            TextView categoryNameTextView = (TextView)(item.findViewById(R.id.category_name_tv));

 

猜你喜欢

转载自hz-chenwenbiao-91.iteye.com/blog/2068349