如何使用ConstraintSet在动态视图中附加其他视图?

时间:2019-12-12 06:58:38

标签: android view android-constraintlayout constraintset

  

我想使用ConstraintSet将动态视图旁边的另一个视图对齐。但是它不能正常工作。下面是我的代码。当我创建想要的动态视图并长按时,旁边的另一个视图将通过动态视图对齐。救救我!

    public class Fit_Me_page extends AppCompatActivity implements View.OnTouchListener, View.OnLongClickListener {

private RecyclerView fit_me_recyclerview;

private ArrayList<Fit_Me_Array> fit_me_arrays;

private Fit_Me_Adapter fit_me_adapter;

Fit_Me_Category_Array fit_me_category_array;

int category_number = 0;

ConstraintLayout fit_me_constraintLayout;

private ConstraintSet applyConstraintSet = new ConstraintSet();

TextView textView, textView2;

ImageView imageView;
ImageView testview;


int i = 11;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fit_me_page);

    fit_me_recyclerview = findViewById(R.id.fit_me_recyclerview);


    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);

    fit_me_arrays = new ArrayList<>();

    fit_me_adapter = new Fit_Me_Adapter(this, fit_me_arrays);

    fit_me_recyclerview.setAdapter(fit_me_adapter);

    fit_me_recyclerview.addItemDecoration(new grid_view_padding(this));

    fit_me_recyclerview.setLayoutManager(gridLayoutManager);

    fit_me_constraintLayout = findViewById(R.id.fit_me_ConstraintLayout);

    textView = findViewById(R.id.textView5);
    textView2 = findViewById(R.id.textView2);


    textView.setOnTouchListener(this::onTouch);

    textView2.setOnTouchListener(this::onTouch);

    fit_me_category_array = new Fit_Me_Category_Array();

for(int i = 0; i

        Fit_Me_Array fit_me_array = new Fit_Me_Array(fit_me_category_array.category_all().get(i));
        fit_me_arrays.add(fit_me_array);

    }

    fit_me_adapter.notifyDataSetChanged();


    fit_me_adapter.setOnItemClickListener(new Fit_Me_Adapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

            imageView = new ImageView(Fit_Me_page.this);


            imageView.setImageResource(R.drawable.color_wheel);

// imageView.setLayoutParams(layoutParams);

            imageView.bringToFront();

            imageView.setOnTouchListener(Fit_Me_page.this::onTouch);

            imageView.setOnLongClickListener(Fit_Me_page.this::onLongClick);

            i++;

            imageView.setId(i);

            imageView.setTag("aaaa");

            fit_me_constraintLayout.addView(imageView);

            applyConstraintSet.clone(fit_me_constraintLayout);
            applyConstraintSet.connect(imageView.getId(), ConstraintSet.TOP, fit_me_constraintLayout.getId(), ConstraintSet.TOP, 460);

            Log.d("imageView_id", "onItemClick:"+imageView.getId());
            applyConstraintSet.connect(textView2.getId(), ConstraintSet.LEFT, imageView.getId(), ConstraintSet.RIGHT, 10);

            applyConstraintSet.connect(textView2.getId(), ConstraintSet.TOP, imageView.getId(), ConstraintSet.TOP, 10);

            applyConstraintSet.applyTo(fit_me_constraintLayout);


        });

    }

    float oldXvalue;

    float oldYvalue;

    @Override
    public boolean onTouch(View v, MotionEvent event){

        int parentWidth = ((ViewGroup)v.getParent()).getWidth(); 
        int parentHeight = ((ViewGroup)v.getParent()).getHeight(); 

        if(event.getAction() == MotionEvent.ACTION_DOWN){


            oldXvalue = event.getX();
            oldYvalue = event.getY();

            return false;

        }else if(event.getAction() == MotionEvent.ACTION_MOVE){

            v.setX(v.getX()+(event.getX())-oldXvalue);

            v.setY(v.getY()+(event.getY())-oldYvalue);

            Log.d("viewTest", "onTouch: ");
            Log.d("viewTest", "onTouch: ");

        }else if(event.getAction() == MotionEvent.ACTION_UP){

            if(v.getX() < 0){
                v.setX(0);
            }else if((v.getX() + v.getWidth()) > parentWidth){
                v.setX(parentWidth - v.getWidth());
            }

            if(v.getY() < 0){
                v.setY(0);
            }else if((v.getY() + v.getHeight()) > parentHeight){
                v.setY(parentHeight - v.getHeight());
            }

            return true;
        }
        return false;
    }

    @Override
    public boolean onLongClick(View v) {

        Toast.makeText(getApplicationContext(), "롱터치"+v.getId()+v.getTag(), Toast.LENGTH_SHORT ).show();

        applyConstraintSet.clone(fit_me_constraintLayout);


        Log.d("imageView_id long_click", "onItemClick:"+v.getId());
        applyConstraintSet.connect(textView2.getId(), ConstraintSet.LEFT, v.getId(), ConstraintSet.RIGHT, 10);

        applyConstraintSet.connect(textView2.getId(), ConstraintSet.TOP, v.getId(), ConstraintSet.TOP, 10);

        applyConstraintSet.connect(textView2.getId(), ConstraintSet.BOTTOM, v.getId(), ConstraintSet.BOTTOM, 10);

        applyConstraintSet.applyTo(fit_me_constraintLayout);

        return true;
    }

}

0 个答案:

没有答案