我正在尝试设置一个显示图像按钮网格的网格视图。我还设置了一个拖动图层,以便您可以单击并将按钮拖动到不同的位置。我的问题是网格视图不会显示图像按钮。 以下是设置布局的两个xml文件。 main.xml和row_grid.xml位于res / layout。
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<edu.purdue.app.DragLayer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drag_layer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="@integer/num_columns"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="0.8"
android:background="@color/light_gray"
/>
</edu.purdue.app.DragLayer>
row_grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageButton
android:id="@+id/MapButton"
android:layout_width="104dp"
android:layout_height="92dp"
android:gravity="center_horizontal|bottom"
android:src="@drawable/ic_launcher" >
</ImageButton>
</LinearLayout>
主要活动:
public class PurdueAppActivity extends Activity implements View.OnLongClickListener, View.OnClickListener,
View.OnTouchListener{
public static final boolean Debugging = false;
DragController mDragController; // Object that handles a drag-drop sequence. It intersacts with DragSource and DropTarget objects.
DragLayer mDragLayer; // The ViewGroup within which an object can be dragged.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.button_grid_view);
gridView.setAdapter (new ImageButtonCellAdapter(this,R.layout.row_grid));
// gridView.setOnItemClickListener (this);
mDragController = new DragController(this);
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController (mDragController);
mDragLayer.setGridView (gridView);
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return startDrag (v);
}
public boolean startDrag (View v)
{
DragSource dragSource = (DragSource) v;
// We are starting a drag. Let the DragController handle it.
mDragController.startDrag (v, dragSource, dragSource, DragController.DRAG_ACTION_MOVE);
return true;
}
}
最后,这是适配器类'getView()方法,应该设置它:
public View getView (int position, View convertView, ViewGroup parent)
{
mParentView = parent;
ImageButtonCell v = null;
if (convertView == null) {
// If it's not recycled, create a new ImageCell.
v = new ImageButtonCell (mContext);
v.setLayoutParams(new GridView.LayoutParams(85, 85));
v.setScaleType(ImageView.ScaleType.CENTER_CROP);
v.setPadding(8, 8, 8, 8);
} else {
v = (ImageButtonCell) convertView;
}
v.mCellNumber = position;
v.mGrid = (GridView) mParentView;
v.mEmpty = true;
// v.setBackgroundResource (R.color.drop_target_enabled);
v.setBackgroundResource (R.color.light_gray);
//v.mGrid.requestDisallowInterceptTouchEvent (true);
//v.setImageResource (R.drawable.hello);
// Set up to relay events to the activity.
// The activity decides which events trigger drag operations.
// Activities like the Android Launcher require a long click to get a drag operation started.
v.setOnTouchListener ((View.OnTouchListener) mContext);
v.setOnClickListener ((View.OnClickListener) mContext);
v.setOnLongClickListener ((View.OnLongClickListener) mContext);
return v;
}
答案 0 :(得分:0)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_grid, null);
}
ImageButton imagebtn = (ImageButton)v.findViewById(R.id.MapButton);
//do something on imagebtn
return v;
}