Android拖放Imageview(这是GridView的一部分)

时间:2017-04-20 10:56:03

标签: android gridview drag-and-drop imageview

我有一个包含6 * 8个ImageView的GridView。我现在希望能够拖动1个ImageView并将其位置与另一个ImageView交换。 我尝试了不适合我的OnDrag方法。我现在用onTouch-Method尝试了它,但是它也没有像计划那样工作。

你们其中一个人是否已经遇到过这个问题,或者可以告诉我如何实施它?

public class GameActivity extends AppCompatActivity implements View.OnTouchListener{
private RelativeLayout rootLayout;
private GridLayout gl_gamefield;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
createGameField(6,8); // columns, rows
}

public void createGameField(int columns, int rows){
    gl_gamefield = new GridLayout(this);
    RelativeLayout.LayoutParams paramsGameField = new RelativeLayout.LayoutParams(getScreenSize()[0],(int)(getScreenSize()[1]-gameFieldFactor));
    paramsGameField.addRule(RelativeLayout.ABOVE, R.id.ad_view);
    paramsGameField.setMargins(0,0,0,20);
    gl_gamefield.setLayoutParams(paramsGameField);
    gl_gamefield.setRowCount(rows);
    gl_gamefield.setColumnCount(columns);
    rootLayout.addView(gl_gamefield);

    ImageView[][] fields = new ImageView[columns][rows];
    LinearLayout.LayoutParams layoutFields =new LinearLayout.LayoutParams((int)(getScreenSize()[0]/columns),(int)((getScreenSize()[1]-gameFieldFactor)/rows));
    int[][] colorFields = new int[columns][rows];

    for(int y = 0; y < rows; y++){
        for(int x = 0; x < columns; x++){
            fields[x][y] = new ImageView(this);
            fields[x][y].setLayoutParams(layoutFields); 
            fields[x][y].setBackgroundColor(Color.rgb(30*x,20*y,10*(x+y)));
            fields[x][y].setTag(x +""+ y);
            fields[x][y].setOnTouchListener(this);
            gl_gamefield.addView(fields[x][y]); 
        }
    }
}

public int[] getScreenSize(){
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    int[] screenSize = {width, height};

    return screenSize;
}

public boolean onTouch(final View v, final MotionEvent event) {

    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:

            if(cdt_running) cdt.cancel();

            cdt_running=true;
            cdt = new CountDownTimer(30000, 20) {

                public void onTick(long millis) {
                    x=event.getRawX()-(v.getWidth()/2);
                    y=event.getRawY()-(v.getHeight()/2);
                    v.setX(x);
                    v.setY(y);

                }
                public void onFinish() {
                    cdt_running=false;
                }
            }.start();


            //ToastMsg("Action_down");
            break;
        case MotionEvent.ACTION_UP:
            ToastMsg("Action_up");
            break;

    }

    return false;
}

这是我最后一次onTouch-Method。但它并没有像我想要的那样安静地工作。

如果有人可以帮助我,我会很高兴,即使这可能是一个愚蠢的问题,因为我没有真正的Android开发经验。

1 个答案:

答案 0 :(得分:0)

我认为这只适合你......

https://github.com/mrKlar/PagedDragDropGrid