1.GridView在xml中的常用屬性
<GridView
android:id="@+id/menu_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#e3e3e3"
android:cacheColorHint="#00000000"
android:gravity="center"
android:horizontalSpacing="2dp"
android:listSelector="#00000000"
android:numColumns="4"
android:scrollbars="none"
android:verticalSpacing="0.5dp">
</GridView>
2.GridView的常用屬性集合解釋
1.android:numColumns="auto_fit" //GridView的列數(shù)設(shè)置為自動(dòng)
2.android:columnWidth="90dp" //每列的寬度悠抹,也就是Item的寬度
3.android:stretchMode="columnWidth"http://縮放與列寬大小同步
4.android:verticalSpacing="1dp"http://兩行之間的邊距
5.android:horizontalSpacing="1dp" //兩列之間的邊距
6.android:cacheColorHint="#00000000" //去除拖動(dòng)時(shí)默認(rèn)的黑色背景
7.android:listSelector="#00000000" //去除選中時(shí)的黃色底色
8.android:scrollbars="none" //隱藏GridView的滾動(dòng)條
9.android:fadeScrollbars="true" //設(shè)置為true就可以實(shí)現(xiàn)滾動(dòng)條的自動(dòng)隱藏和顯示
10.android:fastScrollEnabled="true" //GridView出現(xiàn)快速滾動(dòng)的按鈕(至少滾動(dòng)4頁(yè)才會(huì)顯示)
11.android:fadingEdge="none" //GridView衰落(褪去)邊緣顏色為空书聚,缺省值是vertical备埃。(可以理解為上下邊緣的提示色)
12.android:fadingEdgeLength="10dip" //定義的衰落(褪去)邊緣的長(zhǎng)度
13.android:stackFromBottom="true" //設(shè)置為true時(shí),你做好的列表就會(huì)顯示你列表的最下面
14.android:transcriptMode="alwaysScroll" //當(dāng)你動(dòng)態(tài)添加數(shù)據(jù)時(shí)啼肩,列表將自動(dòng)往下滾動(dòng)最新的條目可以自動(dòng)滾動(dòng)到可視范圍內(nèi)
15.android:drawSelectorOnTop="false" //點(diǎn)擊某條記錄不放,顏色會(huì)在記錄的后面成為背景色,內(nèi)容的文字可見(jiàn)(缺省為false)
drawable的各個(gè)屬性
參考鏈接:(http://blog.csdn.net/czd3355/article/details/52691067)
(http://keeganlee.me/post/android/20150905)
android:drawable 放一個(gè)drawable資源
android:state_pressed 是否按下务漩,如一個(gè)按鈕觸摸或者點(diǎn)擊巡蘸。
android:state_focused 是否取得焦點(diǎn),比如用戶選擇了一個(gè)文本框俭驮。
android:state_hovered 光標(biāo)是否懸停,通常與focused state相同,它是4.0的新特性
android:state_selected 被選中馅而,它與focus state并不完全一樣,如一個(gè)list view 被選中的時(shí)候譬圣,它里面的各個(gè)子組件可能通過(guò)方向鍵瓮恭,被選中了。
android:state_checkable 組件是否能被check厘熟。如:RadioButton是可以被check的屯蹦。
android:state_checked 被checked了,如:一個(gè)RadioButton可以被check了绳姨。
android:state_enabled 能夠接受觸摸或者點(diǎn)擊事件
android:state_activated 被激活(這個(gè)麻煩舉個(gè)例子登澜,不是特明白)
android:state_window_focused 應(yīng)用程序是否在前臺(tái),當(dāng)有通知欄被拉下來(lái)或者一個(gè)對(duì)話框彈出的時(shí)候應(yīng)用程序就不在前臺(tái)了
3.項(xiàng)目實(shí)際應(yīng)用
場(chǎng)景:使用到GridView格式的菜單導(dǎo)航飘庄,需求加上分割線脑蠕。
分析:
1.定義好GridView在xml中的常用屬性,并設(shè)置上背景色跪削,作為分割線的顏色谴仙。
2.GridView的布局的item布局,可以設(shè)置一個(gè)自定義的selector的drawable文件碾盐,點(diǎn)擊的時(shí)候切換顏色顯示晃跺。
3.返回個(gè)數(shù)整除行數(shù)。不夠添加空的Entity對(duì)象毫玖,避免菜單個(gè)數(shù)不夠背景為分割線的灰色掀虎。注意判斷空指針
原理:自定義view幾個(gè)常見(jiàn)的需要注意的方法onMeasure()
、onDraw()
付枫、和onLayout()
烹玉;
onMeasure()
方法中測(cè)量出自定義控件的寬和高并且調(diào)用onLayout()
方法來(lái)確定自定義控件在布局中的位置onDraw()
方法來(lái)將自定義 view 繪制在布局中
所以在未能獲取某個(gè)view的控件寬高時(shí)需要measure
一下測(cè)了出控件的寬高。
4.代碼
1.selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true" >
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"
/>
</shape>
</item>
<item
android:state_pressed="true" >
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"
/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"
/>
</shape>
</item>
</selector>
int i = menuGridList.size() % 4;
if(i!=0){
for (int j = 0; j <4- i; j++) {
menuGridList.add(new MenuEntity());
}
}
menuGridviewAdapter.notifyDataSetChanged();
package com.sinovatech.wanda.sg.business.basic.adapter;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sinovatech.wanda.sg.App;
import com.sinovatech.wanda.sg.R;
import com.sinovatech.wanda.sg.business.basic.entity.MenuEntity;
import com.sinovatech.wanda.sg.business.basic.manager.HttpManager;
import com.sinovatech.wanda.sg.common.GlideApp;
import com.sinovatech.wanda.sg.common.UIUtils;
import java.util.List;
/**
* Created by cheng.qx on 2018/3/6.
*/
public class MainMenuGridviewAdapter extends BaseAdapter {
private Activity mContext;
private LinearLayout mLinearLayout;
private int measureHeight;
private List<MenuEntity> menuList;
public MainMenuGridviewAdapter(Activity context, List<MenuEntity> menuList) {
this.mContext = context;
this.menuList = menuList;
}
/**
* 動(dòng)態(tài)計(jì)算每個(gè)item的高度和寬度励背,GridView不能滾動(dòng)
* @param view
*/
public void getMeasureLayoutHeight(LinearLayout view) {
mLinearLayout = view;
mLinearLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// measureHeight=UIUtils.getScreenHeight(mContext)-mLinearLayout.getMeasuredHeight()-UIUtils.getStatusBarHeight(mContext);
measureHeight = UIUtils.getScreenHeight(mContext) - mLinearLayout.getMeasuredHeight() - 2;//多減去2像素的分割線春霍,可去掉如果需求能滾動(dòng)
}
@Override
public int getCount() {
return menuList.size() ;
}
@Override
public Object getItem(int position) {
return menuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout) mContext.getLayoutInflater().inflate(R.layout.main_menu_item, null);
//動(dòng)態(tài)計(jì)算每個(gè)item的占剩下屏幕高度和寬度,GridView不能滾動(dòng),每行列數(shù)為4的情況下
convertView.setLayoutParams(new AbsListView.LayoutParams(UIUtils.getScreenWidth(mContext) / 4,
measureHeight*4 / (menuList.size() + menuList.size() % 4)));
LinearLayout menuLayout = (LinearLayout) convertView.findViewById(R.id.home_menu_layout);
TextView menuNameText = (TextView) convertView.findViewById(R.id.home_menu_name_text);
ImageView menuImage = convertView.findViewById(R.id.home_menu_imageview);
TextView menuCount = (TextView) convertView.findViewById(R.id.home_menu_notice_view);
final MenuEntity entity = menuList.get(position);
menuNameText.setText(entity.getMenuName());
GlideApp.with(mContext)
.load(HttpManager.ApplicationServer_BASE + entity.getMenuImgUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(menuImage);
menuCount.setText(entity.getMenuCount());
menuCount.setVisibility(View.INVISIBLE);
if (!TextUtils.isEmpty(entity.getMenuCount()) && !"0".equals(entity.getMenuCount())) {
menuCount.setVisibility(View.VISIBLE);
} else {
menuCount.setVisibility(View.INVISIBLE);
}
return convertView;
}
}
GridView中的item布局
<?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:background="@color/color_white"http://可設(shè)置為selector中的drawable,存在點(diǎn)擊效果
android:orientation="vertical">
<LinearLayout
android:id="@+id/home_menu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:clipChildren="false"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="@dimen/new_home_menu_icon_wh"
android:layout_height="@dimen/new_home_menu_icon_wh"
android:layout_marginTop="6dp">
<ImageView
android:id="@+id/home_menu_imageview"
android:layout_width="@dimen/new_home_menu_icon_wh"
android:layout_height="@dimen/new_home_menu_icon_wh"
android:scaleType="fitXY"
android:src="@drawable/btn_09" />
<TextView
android:id="@+id/home_menu_notice_view"
android:layout_width="20dp"
android:layout_height="14dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="-16dp"
android:layout_marginTop="-5dp"
android:background="@drawable/bg_news"
android:gravity="center"
android:text=""
android:textColor="#ffffff"
android:textSize="11sp"
android:visibility="invisible" />
</RelativeLayout>
<TextView
android:id="@+id/home_menu_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="文本標(biāo)題"
android:textColor="#333333"
android:textSize="@dimen/new_home_menu_textsize" />
</LinearLayout>
</LinearLayout>