android gridview畫分割線


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>
 
 http://blog.163.com/www_iloveyou_com/blog/static/211658372201561721245909/
 
#############################################################################
##############################################################################
 

Android控件GridView之仿支付寶錢包首頁帶有分割線的GridView九宮格的完美實現

 分類:

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

     今天我們來模仿一下支付寶錢包首頁中帶有分割線的GridView,俗稱九宮格。先上圖,是你想要的效果么?如果是請繼續往下看。

                                                         

          我們都知道ListView設置分割線是非常容易的,設置ListView的分割線顏色和寬度,只需要在布局中定義android:divider和android:dividerHeight屬性即可。而GridView並沒有這樣的屬性和方法,那我們改如何來做呢?

       博主在做這個效果之前,也參考了其他的一些方案,比如說定義一個自定義的GridView,然后在dispatchDraw()方法中在每個item的四周加上一條分割線,這是需要靠算法來實現的,最后這種方法實現的效果並不理想,會出現有些item中沒有加上分割線,很難達到我們想要的這種效果。

       其實實現這種效果並不難,原理就是讓每個item都設置成帶有分割線的背景,這樣就很容易實現了。

       首先我們來寫布局: 

[html]  view plain copy
 
 
  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.      
  6.      <ScrollView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:fillViewport="true"  
  10.         android:scrollbars="none" >  
  11.   
  12.         <com.finddreams.alipay.MyGridView  
  13.             android:id="@+id/gridview"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:horizontalSpacing="0.0dip"  
  17.             android:listSelector="@null"  
  18.             android:numColumns="3"  
  19.             android:scrollbars="none"  
  20.             android:stretchMode="columnWidth"  
  21.             android:verticalSpacing="0.0dip" />  
  22.     </ScrollView>  
  23.   
  24. </LinearLayout>  

 

       因為有時候我們的Gridview中的item可能比較多,為了放得下,一般都會用一個ScrollView來嵌套起來。這時就會出現一個常見的問題,我們在開發中經常會碰到,就是當ListView或者GridView被嵌套在ScrollView中時,發現只會顯示第一行的數據,后面的數據就不會顯示了。至於產生這個問題的原因,可能是因為Gridview和ListView都是可以根據子item的寬高來顯示大小的,但是一旦嵌套到ScrollView中就可以上下滑動,於是系統就不能確定到底該畫多大,所以才會產生這樣的問題。

      這個問題的解決方法在網上很多,一般百度一下就能查到,下面是GridView的解決方法:

 

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

      接下來,我們就定義一個帶分割線的選擇器,具體代碼是:

 

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

         定義一個selector,在里面設置一個形狀為矩形rectangle,設置這個矩形的stroke描邊屬性的顏色為分割線的顏色,然后在不同的state的item中設置不同的gradient漸變屬性,從而實現在單個item在被點擊選中時的效果。

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

[html]  view plain copy
 
 
  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.   
  8.     <RelativeLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_centerInParent="true"  
  12.         android:background="@drawable/bg_gv"  
  13.         android:padding="12.0dip" >  
  14.   
  15.         <ImageView  
  16.             android:id="@+id/iv_item"  
  17.             android:layout_width="58.0dip"  
  18.             android:layout_height="58.0dip"  
  19.             android:layout_centerHorizontal="true"  
  20.             android:contentDescription="@string/app_name" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tv_item"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_below="@id/iv_item"  
  27.             android:layout_centerHorizontal="true"  
  28.             android:layout_marginTop="5.0dip"  
  29.             android:maxLines="1"  
  30.             android:textColor="@color/commo_text_color"  
  31.             android:textSize="14.0sp" />  
  32.     </RelativeLayout>  
  33.   
  34. </RelativeLayout>  


      到這里,就要開始寫代碼了,定義一個Adapter,把數據填充到GridView中,這一步我想大家都應該都很清楚,這里就不多講了,不懂的話,可以參考下面的項目代碼。

     項目鏈接:http://download.csdn.net/detail/finddreams/8423263    給有需要的朋友!

 
3
1
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM