【问题标题】:Setting ontouchlistener on multiple dynamic buttons with flow layout在具有流布局的多个动态按钮上设置 ontouchlistener
【发布时间】:2017-06-28 10:19:42
【问题描述】:

我正在使用来自参考https://github.com/ApmeM/android-flowlayout 的流布局。现在我在此布局上创建多个动态按钮并在它们上设置 ontouchlistener。问题是当我通过触摸更改一个按钮的位置时,其他按钮也是改变那里的位置。我想改变我正在触摸的唯一按钮的位置。有没有办法做到这一点或其他解决方案。

public class MainActivity extends AppCompatActivity implements    View.OnTouchListener {
          Button floatButton;
        Button _view;
        private int _xDelta;
        private int _yDelta;
        ViewGroup layout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            layout = (ViewGroup) findViewById(R.id.flowLayout);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = 50;
            layoutParams.topMargin = 50;
            layoutParams.bottomMargin = 0;
            layoutParams.rightMargin = 0;
            for (int i = 0; i < 10; i++) {
                _view = new Button(this);
                _view.setText("Button"+(i+1));
                _view.setLayoutParams(layoutParams);
               _view.setOnTouchListener(this);
                layout.addView(_view);
            }
        }
        public boolean onTouch(View view, MotionEvent event) {
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            try {
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:

                        FlowLayout.LayoutParams lParams = (FlowLayout.LayoutParams) view.getLayoutParams();
                        _xDelta = X - lParams.leftMargin;
                        _yDelta = Y - lParams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        FlowLayout.LayoutParams layoutParams = (FlowLayout.LayoutParams) view.getLayoutParams();
                        layoutParams.leftMargin = X - _xDelta;
                        layoutParams.topMargin = Y - _yDelta;
                        layoutParams.rightMargin = 0;
                        layoutParams.bottomMargin = 0;
                        view.setLayoutParams(layoutParams);
                        break;
                }
            }catch (Exception ex){
                Log.d("ON Touch ", ex.toString());
            }
            layout.invalidate();
            return true;
        }
    }

【问题讨论】:

    标签: android android-layout ontouchlistener flowlayout dynamic-view


    【解决方案1】:

    这很可能是因为您使用的是 RelativeLayout 并且其他对象位置以某种方式相对于您正在移动的按钮定义。如果是这种情况,那么您描述的行为是预期的。但我们需要查看 XML 文件才能准确了解发生了什么。

    【讨论】:

    • 是否有任何其他解决方案我只想将动态按钮添加到屏幕并希望它们在触摸监听器上实现。
    • 您仍然可以使用相对布局,但您必须相对于父项而不是彼此来定位项目。或者,如果您只想移动几个按钮,您可以尝试使用框架布局将它们分层。
    • 谢谢 bremen_matt。如果我从 xml 布局文件中膨胀一个按钮,你能解释一下如何相对于父项定位项目。
    猜你喜欢
    • 2013-03-25
    • 2017-07-22
    • 2014-11-04
    • 2014-12-05
    • 2015-12-18
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多