android gridview畫分割線,如圖:


1。先上圖: android gridview畫分割線,如圖: - dongyangzhang - 軟件開發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>

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com