OnClickListener无法以编程方式添加视图

时间:2016-07-31 21:16:26

标签: android android-layout

好的,我有一个名为" AddView()"当我长按时,一个矩形显示在我的手指位置,这很好。

问题出在" selectView()"方法。我尝试过使用OnTouchListener和OnLongClickListener,但没有任何反应......

当我长时间按下我使用" AddView"

创建的视图时,我只想要一个简单的Toast弹出
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

View _view;
ViewGroup _root;
private int _yDelta;
private int positionY;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    _root = (ViewGroup)findViewById(R.id.root);
    _view = new View(this);

    addView();
    selectView();
}

private void selectView() {

    _view.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            Toast.makeText(MainActivity.this, "Worked?", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    _view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this, "Worked?", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

private void addView() {

    _root.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            positionY = (int)event.getY();
            return false;
        }
    });

    _root.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            _view = new View(MainActivity.this);
            _view.setBackgroundColor(0xFF00FF00);
            _view.setLayoutParams(new RelativeLayout.LayoutParams(1080, 100));
            _view.setTranslationX(0);
            _view.setTranslationY(positionY - 80);
            _view.setOnTouchListener(MainActivity.this);
            _view.setLongClickable(true);
            _root.addView(_view);

            return true;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

添加视图时,永远不会设置长按侦听器。

试试这样

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    _root = (ViewGroup)findViewById(R.id.root);

    _root.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            positionY = (int)event.getY();
            return false;
        }
    });

    addViewOnTouch(_root);
}

private void setTouchListener(final View touched) {

    touched.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            Toast.makeText(MainActivity.this, "Worked?", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    touched.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this, "Worked?", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

private void addViewOnTouch(final View touched) {

    touched.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            View _view = new View(MainActivity.this);
            _view.setBackgroundColor(0xFF00FF00);
            _view.setLayoutParams(new RelativeLayout.LayoutParams(1080, 100));
            _view.setTranslationX(0);
            _view.setTranslationY(positionY - 80);
            _view.setOnTouchListener(MainActivity.this);
            _view.setLongClickable(true);

            setTouchListener(_view); // Need to set touch on new View

            touched.addView(_view);
            return true;
        }
    });
}