androidjava
咱們有時作一個菜單,點擊後,背景變爲點擊後色,效果以下:android
這裏只是簡單介紹實現過程:app
定義存放菜單的listview:ide
<ListView this
android:id="@+id/subject_menu_category_lv" spa
android:layout_width="match_parent" orm
android:layout_height="match_parent" xml
android:layout_above="@id/subject_menu_bottom_layout" blog
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));