2.具体实现代码:
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>
本文转自 一点点征服 博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/5647651.html,如需转载请自行联系原作者