如何在创建的动态视图中附加另一个视图?

时间:2019-12-11 15:50:35

标签: android android-layout view constraints add

我是android的新手。 我想在创建的动态视图旁边放置一个新视图,但是我不知道如何。 单击创建的动态视图时,希望在其旁边显示另一个视图。 另外,如果可能的话,移动一个视图时,其旁边的另一个视图也应移动。

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_FrameLayout;
    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_FrameLayout = findViewById(R.id.fit_me_ConstraintLayout);

        TextView textView = findViewById(R.id.textView5);
        TextView 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_category_array.category_all().size(); 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) {

                if(position==1&&category_number==0){
                       fit_me_arrays.clear();

                       for (int i =0; i<fit_me_category_array.Top().size(); i++){
                           Fit_Me_Array fit_me_array = new Fit_Me_Array(fit_me_category_array.Top().get(i));
                           fit_me_arrays.add(fit_me_array);
                       }

                       category_number++;

                       fit_me_adapter.notifyDataSetChanged();
                   }
                   else if(position==1&&category_number==1){
                       fit_me_arrays.clear();

                       for (int i =0; i<fit_me_category_array.Top2().size(); i++){
                           Fit_Me_Array fit_me_array = new Fit_Me_Array(fit_me_category_array.Top2().get(i));
                           fit_me_arrays.add(fit_me_array);
                       }

                       fit_me_adapter.notifyDataSetChanged();
                       category_number++;
                   }
                   else if(position==1&&category_number==2){
                       Toast.makeText(getApplicationContext(), "포지션값"+ position, Toast.LENGTH_SHORT).show();

                       FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(300,500);

                       ImageView imageView = new ImageView(Fit_Me_page.this);

                       imageView.setImageResource(R.drawable.color_wheel);
                      // imageView.setBackgroundColor(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("가가가");

                       fit_me_FrameLayout.addView(imageView);
                   }

                   if(position==0&&category_number==2){
                       fit_me_arrays.clear();

                       for (int i =0; i<fit_me_category_array.Top().size(); i++){
                           Fit_Me_Array fit_me_array = new Fit_Me_Array(fit_me_category_array.Top().get(i));
                           fit_me_arrays.add(fit_me_array);

                       }

                       fit_me_adapter.notifyDataSetChanged();

                       category_number--;

                   }

                   else if(position==0&&category_number==1){
                        fit_me_arrays.clear();

                        for (int i =0; i<fit_me_category_array.category_all().size(); 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();

                    category_number--;

                }
            }
        });
    }

    float oldXvalue;
    float oldYvalue;

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

        int parentWidth = ((ViewGroup)v.getParent()).getWidth();    // View Width
        int parentHeight = ((ViewGroup)v.getParent()).getHeight();    // View Height

        if(event.getAction() == MotionEvent.ACTION_DOWN){
            Log.d("바로?", "onTouch: "+"다운");
            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) {
        return true;
    }
}

0 个答案:

没有答案