android gridview画分割线
android gridview画分割线,如图:
1。先上图:

- public class LineGridView extends GridView {
public LineGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}- public LineGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}- public LineGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}- @Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
View localView1 = getChildAt(0);
int column = getWidth() / localView1.getWidth();//计算出一共有多少列,假设有3列
int childCount = getChildCount();//子view的总数
System.out.println("子view的总数childCount==" + childCount);
Paint localPaint;//画笔
localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setColor(getContext().getResources().getColor(R.color.line));//设置画笔的颜色
for (int i = 0; i < childCount; i++) {//遍历子view
View cellView = getChildAt(i);//获取子view
if (i < 3) {//第一行
canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(), localPaint);
}
if (i % column == 0) {//第一列
canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getLeft(), cellView.getBottom(), localPaint);
}
if ((i + 1) % column == 0) {//第三列
//画子view底部横线
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
} else if ((i + 1) > (childCount - (childCount % column))) {//如果view是最后一行
//画子view的右边竖线
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
} else {//如果view不是最后一行
//画子view的右边竖线
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
//画子view的底部横线
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
}
}
}
}
3.main里的方法:
- public class Main2Activity extends Activity {
LineGridView gridview;- @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
gridview = (LineGridView) findViewById(R.id.gridview);
findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
final MyAdapter myAdapter = new MyAdapter();- gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myAdapter.setSelectItem(position);
}
});
gridview.setAdapter(myAdapter);
}- private class MyAdapter extends BaseAdapter {
- @Override
public int getCount() {
return 5;
}- @Override
public Object getItem(int position) {
return null;
}- @Override
public long getItemId(int position) {
return 0;
}- @Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(Main2Activity.this, R.layout.gride_list_item, null);
LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll);
if (position == currentItem) {//选中的item
ll.setBackgroundColor(Main2Activity.this.getResources().getColor(R.color.item_select));
} else {
ll.setBackgroundColor(Main2Activity.this.getResources().getColor(R.color.white));
}
return v;
}- private int currentItem = 0;
- public void setSelectItem(int position) {
currentItem = position;
notifyDataSetChanged();
}
}
4, activity_main2.xml代码:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.zdy.bbs_bbb.clicktoast.Main2Activity">- <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/hello_world"/>- <com.zdy.bbs_bbb.clicktoast.LineGridView
android:padding="3dp"
android:layout_margin="10dp"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:background="#ffffff"
android:gravity="center"
android:listSelector="@null"
android:numColumns="3"
android:stretchMode="columnWidth"
/>
</RelativeLayout>
gride_list_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:gravity="center"
android:id="@+id/ll"
android:background="@color/item_select"
android:orientation= "vertical" >- <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="全部"
/>- </LinearLayout>



关注finddreams:http://blog.csdn.net/finddreams/article/details/43486527
今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。
我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?
博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。
其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。
首先我们来写布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:fillViewport="true"
- android:scrollbars="none" >
- <com.finddreams.alipay.MyGridView
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:horizontalSpacing="0.0dip"
- android:listSelector="@null"
- android:numColumns="3"
- android:scrollbars="none"
- android:stretchMode="columnWidth"
- android:verticalSpacing="0.0dip" />
- </ScrollView>
- </LinearLayout>
因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是系统就不能确定到底该画多大,所以才会产生这样的问题。
这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:
- public class MyGridView extends GridView {
- public MyGridView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyGridView(Context context) {
- super(context);
- }
- public MyGridView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
- MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- }
- }
接下来,我们就定义一个带分割线的选择器,具体代码是:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true"><shape android:shape="rectangle">
- <stroke android:width="1.0px" android:color="@color/line" />
- <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
- </shape></item>
- <item android:state_focused="true"><shape android:shape="rectangle">
- <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
- <stroke android:width="1.0px" android:color="@color/line" />
- </shape></item>
- <item><shape android:shape="rectangle">
- <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
- <stroke android:width="1.0px" android:color="@color/line" />
- </shape></item>
- </selector>
定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。
接着就是给我们GridView的item布局中加上背景了:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="0.0dip"
- android:background="@color/griditems_bg" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_centerInParent="true"
- android:background="@drawable/bg_gv"
- android:padding="12.0dip" >
- <ImageView
- android:id="@+id/iv_item"
- android:layout_width="58.0dip"
- android:layout_height="58.0dip"
- android:layout_centerHorizontal="true"
- android:contentDescription="@string/app_name" />
- <TextView
- android:id="@+id/tv_item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/iv_item"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="5.0dip"
- android:maxLines="1"
- android:textColor="@color/commo_text_color"
- android:textSize="14.0sp" />
- </RelativeLayout>
- </RelativeLayout>
到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。
项目链接:http://download.csdn.net/detail/finddreams/8423263 给有需要的朋友!
- 顶
- 3
- 踩
- 1
- 上一篇Android开发自定义View之滑动按钮与自定义属性
- 下一篇仿微信中加载网页时带线行进度条的WebView的实现
http://blog.csdn.net/jdsjlzx/article/details/44182693
android gridview画分割线的更多相关文章
- android gridview画分割线,如图:
1.先上图: 2.具体实现代码: public class LineGridView extends GridView { public LineGridView(Context context) { ...
- 自定义GridView实现分割线解析
前两天在些项目的时候碰到常用的GridView要实现一些分割线,之前就是用本方法利用listView和Item的背景颜色的不同线显示分割线.这是最low的一种做法.于是我就简单的写了一个自定义的 Gr ...
- Android GridView 添加 网格线解决办法
在使用 GridView 网格布局时,默认情况下 GridView 是没有网格线的,但是有些时候我们需要让GridView显示分割线,怎么办呢?查了不少资料,找到了一种为GridView添加网格线的 ...
- Android——GridView(网格视图)相关知识总结贴
Android API中文文档GridView http://www.apkbus.com/android-14131-1-1.html Android API 中文 (15) —— GridVi ...
- android listview去掉分割线
1:android listview去掉分割线 1>设置android:divider="@null" 2>android:divider="#0000000 ...
- ANDROID SHAPE画圆形背景_ANDROID实现角标布局
ANDROID SHAPE画圆形背景_ANDROID实现角标布局 <?xml version="1.0" encoding="UTF-8"?> &l ...
- Android GridView增加HeaderView和FooterView的实现
Android GridView增加HeaderView和FooterView的实现 做的项目中遇到一个问题,需要实现一个页面 页面的上面是一个自定义的View和GridView,当向下滚动屏幕的时候 ...
- android gridview 停止滚动
http://blog.csdn.net/yaphetzhao/article/details/50544105 参考上面的博客,关键代码我就贴出来吧: public void stopGridVie ...
- [Android]ListView中分割线的设置
1.在布局文件中ListView元素中通过属性设置 android:divider="#fffff" 分割线颜色 android:dividerHeight="1px& ...
随机推荐
- 【Python初学】深copy&浅copy
在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. copy.copy 浅拷贝 只拷贝父对象 ...
- Android多线程下安全访问数据库
http://zhiwei.neatooo.com/blog/detail?blog=5343818a9d4869f0310000de github 原文https://github.com/dmyt ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- 篇一:eclipse创建maven工程
一.概览 maven创建的项目主要分为三类:war(网页工程).jar(Java工程).pom(父工程); war:网页工程,包含webapp,用于view层 jar:Java工程,用于提供方法.se ...
- android studio sexy editor性感编辑器设置
sexy editor下载地址:http://download.csdn.net/detail/yy1300326388/9166223 我自己也有上传CSDN资源 rainyday0524@163. ...
- 移动端h5页面的设计稿尺寸
当我们在做手机端H5网页设计稿时(当然包含微信端的H5网页设计),如果没有做过类似的移动端的设计,UI设计师和前端工程师肯定会纠结的.如果是app设计师,就不会那么纠结啦. 延伸阅读: 2015年度最 ...
- psy & vr
技术指标及应用法则:PSY和VR 一.PSY(Psychologial Line) 1. PSY的参数 2. PSY的应用法则 二.VR(Volume Ratio) 1. VR的参数 2. VR的 ...
- 转:sql SELECT时的with(nolock)选项说明
I used to see my senior developers use WITH (NOLOCK) when querying in SQL Server and wonder why they ...
- 第2课 Linux操作系统简介
1. Linux操作系统的构成 (1)内核(kernel) ①操作系统的核心,负责管理系统的进程.内存.设备驱动程序.文件和网络系统. ②控制系统和硬件之间的相互通信. ③决定着系统的性能和稳定性. ...
- Quartus14.1中Qsys无法更新custom component的问题
如果对Qsys中custom component进行了改动,如果直接generate HDL无法使改动应用到实际的编译过程,需要对Qsys进行刷新操作,如下: