huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.DeviceTest.view;
 
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
 
public class MyGridView extends FrameLayout implements View.OnClickListener {
   private String TAG = "MyGridView";
   private int mColumn = 1;
   private OnItemClickListener onItemClickListener;
 
   public MyGridView(Context context) {
       this(context, null, 0);
   }
 
   public MyGridView(Context context, AttributeSet attrs) {
       this(context, attrs, 0);
   }
 
   public MyGridView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }
 
   public void setColumnCount(int column) {
       this.mColumn = column;
   }
 
   
   protected void onLayout(boolean changed, int left, int top, int right,
           int bottom) {
       final int childCount = getChildCount();
       if (0 == childCount) {
           super.onLayout(changed, left, top, right, bottom);
           return;
       }
       final int width = right - left;
       final int height = bottom - top;
       final int column = mColumn;
       final int row = (childCount - 1) / column + 1;
       final int childHeight = height / row;
       final int childWidth = width / column;
 
       int childLeft = left;
       int childTop = top;
       for (int i = 0; i < childCount; i++) {
           View child = getChildAt(i);
           child.layout(childLeft, childTop, childLeft + childWidth, childTop
                   + childHeight);
           childLeft += childWidth;
           if (((i + 1) % column) == 0) {
               childLeft = left;
               childTop += childHeight;
           }
       }
   }
 
   public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
       this.onItemClickListener = onItemClickListener;
   }
   
   public void addView(View child, int index, ViewGroup.LayoutParams params) {
       super.addView(child, index, params);
       child.setOnClickListener(this);
   }
 
   public interface OnItemClickListener {
       void onItemClick(ViewParent parent, View view, int position);
   }
 
   
   public void onClick(View v) {
       if (null == onItemClickListener) {
           return;
       }
       final int childCount = getChildCount();
       int istouchchildcount = 0;
       int touchid = -1;
       for (int i = 0; i < childCount; i++) {
           if (v == getChildAt(i)) {
               Log.d(TAG, "__________-----------onClick(),      i = " + i);
               touchid = i;
           }
           if(((MyItemView)getChildAt(i)).getIsTouch()){
               istouchchildcount ++;
           }
       }
       Log.d(TAG, "__________-----------onClick(),  touchid = " + touchid + "     istouchchildcount = " + istouchchildcount);
       if(touchid >= 0 && istouchchildcount == 0)
           onItemClickListener.onItemClick(this, v, touchid);
   }
}