2014-02-14 2 views
2

이미지 단추 격자를 표시하는 눈금보기를 설정하려고합니다. 드래그 레이어를 설정하여 버튼을 클릭하여 다른 위치로 드래그 할 수 있어야합니다. 내 문제는 눈금보기 이미지 단추를 표시하지 것입니다. 다음은 레이아웃을 설정하는 두 개의 XML 파일입니다. main.xml 및 row_grid.xml은 res/layout에 있습니다.Gridview가 이미지 단추를 표시하지 않습니다.

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
@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; 
} 
+0

하나 이상의 버튼을 추가하고 싶다면 MapButton에 대해 동일한 전략을 사용하여 올바르게 추가 할 수 있다고 가정합니다. 그리고 그리드 뷰는 모든 레이아웃을 배치합니다. – Otto45

관련 문제