【问题标题】:Dynamically creating multiple imageButtons Makes drag and drop difficult动态创建多个 imageButtons 使拖放变得困难
【发布时间】:2014-08-07 22:41:37
【问题描述】:

我有一个对话框菜单设置供用户选择他们想要建造的建筑物。当他们按下购买按钮时,建筑物会自动插入到活动中。问题是,当购买了不止一个时,移动最近购买的建筑物也会移动第二最近购买的建筑物。我不积极如何解决这个问题。我会看看将新建筑物设置为框架布局而不是线性布局是否会有所帮助,但如果我这样做,我的拖放将不起作用。

这是按下购买按钮后正在设置的图像按钮的代码:

            layout = (LinearLayout) findViewById(R.id.newColonyHutLayout);
            buyColonyHut = (Button) buildingSelect.findViewById(R.id.buyColonyHut);
            buyColonyHut.setText("Buy");
            buyColonyHut.setBackgroundColor(-65536);
            buyColonyHut.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    //get current amount of rock and check if it is enough to buy
                    data = new Database(runGraphics.this);
                    data.open();
                    rockAmount = data.getRockAmount();
                    data.close();
                    if (rockAmount >= 100)
                    {
                        //take away spent money and update database
                        data.open();
                        rockAmount -= 100;
                        data.rockAmountEntry(rockAmount);
                        data.close();

                        //update the scores
                        updateScores();

                        //close the dialog
                        buildingSelect.dismiss();
                        buildMoreButton.setBackgroundColor(-65536);

                        //they have enough rock, build the building
                        newColonyHut = new ImageButton(runGraphics.this);
                        newColonyHut.setBackgroundResource(R.drawable.mainhut);
                        newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                        layout.addView(newColonyHut);
                        newColonyHut.setOnTouchListener(new OnTouchListener()
                        {   
                            @Override
                            public boolean onTouch(View v, MotionEvent event) 
                            {
                                if (event.getAction() == MotionEvent.ACTION_DOWN)
                                {
                                    newColonyHutSelected = true;
                                }//end if

                                if (event.getAction() == MotionEvent.ACTION_UP)
                                {
                                    if (newColonyHutSelected == true)
                                    {
                                        newColonyHutSelected = false;
                                    }//end if
                                }//end if
                                else if (event.getAction() == MotionEvent.ACTION_MOVE)
                                {
                                    if (newColonyHutSelected == true)
                                    {
                                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
                                        params.setMargins((int)(event.getRawX() - event.getRawY() / 5), (int) (event.getRawY() - v.getHeight()), (int) (event.getRawX() - v.getWidth() / 5), (int) (event.getRawY() - v.getHeight()));
                                        v.setLayoutParams(params);
                                    }//end if
                                }//end else

                                return false;
                            }//end onTouch function

                        });//end setOnTouchListener
                    }//end onClick function
                });//end onClickListener

这里是xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/newColonyHutLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/newColonyHut"
            android:visibility="invisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/mainhut" />
    </LinearLayout>
</FrameLayout>

【问题讨论】:

标签: java android drag-and-drop imagebutton android-framelayout


【解决方案1】:

尝试在onTouch()方法结束时返回true,以通知TouchEvent已被监听器消费。否则,将调用被触摸的 View 下面的 View 的后续侦听器 - 例如您的第二座建筑之一。

另一个想法是使用View.onDragListener,而不是完全自己实现拖放。这是一个如何使用它的好例子:http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html#draganddrop_overview

【讨论】:

  • 如果他返回true,它将消耗ACTION_DOWN的事件并且不允许ACTION_MOVE。
  • 好的,不如使用 View.OnDragListener 而不是完全自己实现拖放操作?
  • 这是一个不错的选择,易于实现,但需要 API 11 及以上。但是,这是一个替代方案。
  • 所以你是说使用 View.OnDragListener 将在框架布局中工作?或者您是说它会在线性布局中自行停止先前创建的图像按钮的移动?
  • @saboehnke 你是否在框架布局中膨胀每个视图?并为每个人添加触摸监听器?
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2023-01-09
相关资源
最近更新 更多