android gridview画分割线,如图:

1。先上图:

2.具体实现代码:
  1. public class LineGridView extends GridView {
    public LineGridView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    }
  2.  
  3. public LineGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }
  4.  
  5. public LineGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }
  6.  
  7. @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里的方法:

  1. public class Main2Activity extends Activity {
    LineGridView gridview;
  2.  
  3. @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();
  4.  
  5. gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    myAdapter.setSelectItem(position);
    }
    });
    gridview.setAdapter(myAdapter);
    }
  6.  
  7. private class MyAdapter extends BaseAdapter {
  8.  
  9. @Override
    public int getCount() {
    return 5;
    }
  10.  
  11. @Override
    public Object getItem(int position) {
    return null;
    }
  12.  
  13. @Override
    public long getItemId(int position) {
    return 0;
    }
  14.  
  15. @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;
    }
  16.  
  17. private int currentItem = 0;
  18.  
  19. public void setSelectItem(int position) {
    currentItem = position;
    notifyDataSetChanged();
    }
    }

4, activity_main2.xml代码:

  1. <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">
  2.  
  3. <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="@string/hello_world"/>
  4.  
  5. <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

  1. <?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" >
  2.  
  3. <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    />
  4.  
  5. <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="全部"
    />
  6.  
  7. </LinearLayout>
 
 http://blog.163.com/www_iloveyou_com/blog/static/211658372201561721245909/
 
#############################################################################
##############################################################################
2015-03-10 22:38 14659人阅读 评论(12) 收藏 举报
 分类:
Android UI(797) Android开发(1489) 

关注finddreams:http://blog.csdn.net/finddreams/article/details/43486527

今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。

我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?

博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。

其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。

首先我们来写布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <ScrollView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:fillViewport="true"
  9. android:scrollbars="none" >
  10. <com.finddreams.alipay.MyGridView
  11. android:id="@+id/gridview"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:horizontalSpacing="0.0dip"
  15. android:listSelector="@null"
  16. android:numColumns="3"
  17. android:scrollbars="none"
  18. android:stretchMode="columnWidth"
  19. android:verticalSpacing="0.0dip" />
  20. </ScrollView>
  21. </LinearLayout>

因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是系统就不能确定到底该画多大,所以才会产生这样的问题。

这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:

  1. public class MyGridView extends GridView {
  2. public MyGridView(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. }
  5. public MyGridView(Context context) {
  6. super(context);
  7. }
  8. public MyGridView(Context context, AttributeSet attrs, int defStyle) {
  9. super(context, attrs, defStyle);
  10. }
  11. @Override
  12. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  13. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  14. MeasureSpec.AT_MOST);
  15. super.onMeasure(widthMeasureSpec, expandSpec);
  16. }
  17. }

接下来,我们就定义一个带分割线的选择器,具体代码是:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true"><shape android:shape="rectangle">
  4. <stroke android:width="1.0px" android:color="@color/line" />
  5. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  6. </shape></item>
  7. <item android:state_focused="true"><shape android:shape="rectangle">
  8. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  9. <stroke android:width="1.0px" android:color="@color/line" />
  10. </shape></item>
  11. <item><shape android:shape="rectangle">
  12. <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
  13. <stroke android:width="1.0px" android:color="@color/line" />
  14. </shape></item>
  15. </selector>

定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。

接着就是给我们GridView的item布局中加上背景了:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:layout_margin="0.0dip"
  6. android:background="@color/griditems_bg" >
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_centerInParent="true"
  11. android:background="@drawable/bg_gv"
  12. android:padding="12.0dip" >
  13. <ImageView
  14. android:id="@+id/iv_item"
  15. android:layout_width="58.0dip"
  16. android:layout_height="58.0dip"
  17. android:layout_centerHorizontal="true"
  18. android:contentDescription="@string/app_name" />
  19. <TextView
  20. android:id="@+id/tv_item"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/iv_item"
  24. android:layout_centerHorizontal="true"
  25. android:layout_marginTop="5.0dip"
  26. android:maxLines="1"
  27. android:textColor="@color/commo_text_color"
  28. android:textSize="14.0sp" />
  29. </RelativeLayout>
  30. </RelativeLayout>

到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。

项目链接:http://download.csdn.net/detail/finddreams/8423263    给有需要的朋友!

 
3
1

android gridview画分割线的更多相关文章

  1. android gridview画分割线,如图:

    1.先上图: 2.具体实现代码: public class LineGridView extends GridView { public LineGridView(Context context) { ...

  2. 自定义GridView实现分割线解析

    前两天在些项目的时候碰到常用的GridView要实现一些分割线,之前就是用本方法利用listView和Item的背景颜色的不同线显示分割线.这是最low的一种做法.于是我就简单的写了一个自定义的 Gr ...

  3. Android GridView 添加 网格线解决办法

    在使用 GridView 网格布局时,默认情况下 GridView  是没有网格线的,但是有些时候我们需要让GridView显示分割线,怎么办呢?查了不少资料,找到了一种为GridView添加网格线的 ...

  4. Android——GridView(网格视图)相关知识总结贴

    Android API中文文档GridView http://www.apkbus.com/android-14131-1-1.html   Android API 中文 (15) —— GridVi ...

  5. android listview去掉分割线

    1:android listview去掉分割线 1>设置android:divider="@null" 2>android:divider="#0000000 ...

  6. ANDROID SHAPE画圆形背景_ANDROID实现角标布局

    ANDROID SHAPE画圆形背景_ANDROID实现角标布局 <?xml version="1.0" encoding="UTF-8"?> &l ...

  7. Android GridView增加HeaderView和FooterView的实现

    Android GridView增加HeaderView和FooterView的实现 做的项目中遇到一个问题,需要实现一个页面 页面的上面是一个自定义的View和GridView,当向下滚动屏幕的时候 ...

  8. android gridview 停止滚动

    http://blog.csdn.net/yaphetzhao/article/details/50544105 参考上面的博客,关键代码我就贴出来吧: public void stopGridVie ...

  9. [Android]ListView中分割线的设置

    1.在布局文件中ListView元素中通过属性设置 android:divider="#fffff"  分割线颜色 android:dividerHeight="1px& ...

随机推荐

  1. 【Python初学】深copy&浅copy

    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. copy.copy 浅拷贝 只拷贝父对象 ...

  2. Android多线程下安全访问数据库

    http://zhiwei.neatooo.com/blog/detail?blog=5343818a9d4869f0310000de github 原文https://github.com/dmyt ...

  3. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  4. 篇一:eclipse创建maven工程

    一.概览 maven创建的项目主要分为三类:war(网页工程).jar(Java工程).pom(父工程); war:网页工程,包含webapp,用于view层 jar:Java工程,用于提供方法.se ...

  5. android studio sexy editor性感编辑器设置

    sexy editor下载地址:http://download.csdn.net/detail/yy1300326388/9166223 我自己也有上传CSDN资源 rainyday0524@163. ...

  6. 移动端h5页面的设计稿尺寸

    当我们在做手机端H5网页设计稿时(当然包含微信端的H5网页设计),如果没有做过类似的移动端的设计,UI设计师和前端工程师肯定会纠结的.如果是app设计师,就不会那么纠结啦. 延伸阅读: 2015年度最 ...

  7. psy & vr

    技术指标及应用法则:PSY和VR 一.PSY(Psychologial Line)   1. PSY的参数 2. PSY的应用法则 二.VR(Volume Ratio) 1. VR的参数 2. VR的 ...

  8. 转:sql SELECT时的with(nolock)选项说明

    I used to see my senior developers use WITH (NOLOCK) when querying in SQL Server and wonder why they ...

  9. 第2课 Linux操作系统简介

    1. Linux操作系统的构成 (1)内核(kernel) ①操作系统的核心,负责管理系统的进程.内存.设备驱动程序.文件和网络系统. ②控制系统和硬件之间的相互通信. ③决定着系统的性能和稳定性. ...

  10. Quartus14.1中Qsys无法更新custom component的问题

    如果对Qsys中custom component进行了改动,如果直接generate HDL无法使改动应用到实际的编译过程,需要对Qsys进行刷新操作,如下: