繁体   English   中英

如何使用DragEvent以线性布局放置图像

[英]How to place images in a linear layout using DragEvent

我正在尝试实现一个拖动难题,其中将一个图像移动到LinearLayout中以解决难题。

当我将一个图像放置在LinearLayout之一上是很好的,但是我不希望将任何其他图片放置在相同的LinearLayout中时,另一个问题是当我将图像拖动到LinearLayout之外并放下图像时,图像消失了。

package com.example.android.myapplication;

import android.content.ClipData;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class BlankFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    View vista;
    LinearLayout layout1, layout2, layout3, layout4, layout5, layout6;
    ImageView image1, image2, image3;

    private OnFragmentInteractionListener mListener;

    public BlankFragment() 
    {

    }

    public static BlankFragment newInstance(String param1, String param2) 
    {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    class MyTouchListener implements View.OnTouchListener 
    {
        public boolean onTouch(View view, MotionEvent motionEvent) 
        {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) 
            {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    class MyDragListener implements View.OnDragListener 
    {
        Drawable enterShape = getResources().getDrawable(
                R.drawable.shape_droptarget);
        Drawable normalShape = getResources().getDrawable(R.drawable.shape);

        @Override
        public boolean onDrag(View v, DragEvent event) 
        {
            int action = event.getAction();
            switch (event.getAction()) 
            {
                case DragEvent.ACTION_DRAG_STARTED:

                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundDrawable(enterShape);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundDrawable(normalShape);
                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    LinearLayout container = (LinearLayout) v;
                    container.addView(view);
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundDrawable(normalShape);
                default:
                    break;
            }
            return true;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
    {
        // Inflate the layout for this fragment
        vista = inflater.inflate(R.layout.fragment_blank, container, false);

        image1 = vista.findViewById(R.id.image1);
        image1.setOnTouchListener(new BlankFragment.MyTouchListener());

        image2 = vista.findViewById(R.id.image2);
        image2.setOnTouchListener(new BlankFragment.MyTouchListener());

        image3 = vista.findViewById(R.id.image3);
        image3.setOnTouchListener(new BlankFragment.MyTouchListener());

        layout1 = vista.findViewById(R.id.layout1);
        layout1.setOnDragListener(new MyDragListener());

        layout2 = vista.findViewById(R.id.layout2);
        layout2.setOnDragListener(new MyDragListener());

        layout3 = vista.findViewById(R.id.layout3);
        layout3.setOnDragListener(new MyDragListener());

        layout4 = vista.findViewById(R.id.layout4);
        layout4.setOnDragListener(new MyDragListener());

        layout5 = vista.findViewById(R.id.layout5);
        layout5.setOnDragListener(new MyDragListener());

        layout6 = vista.findViewById(R.id.layout6);
        layout6.setOnDragListener(new MyDragListener());

        return vista;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) 
    {
        if (mListener != null) 
        {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) 
    {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) 
        {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() 
    {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener 
    {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

这是我的布局
正常图像

我不希望这种情况发生
同一LinearLayout中的两个图像

每个LinearLayout仅有一个,并且将图像移到LinearLayouts之外会使图像消失的事情

我之前也遇到过同样的问题, 这段代码可能会对您有所帮助。

基本上,您需要一个临时视图来保存拖动的视图,并删除布局中可能已经包含的视图。

 private final class MyTouchListener implements View.OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        for (LinearLayout layout : layouts) {

            if (layout.getChildCount() == 0) {

                layout.setOnDragListener(new MyDragListener());
            }
        }


        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.VISIBLE);

        return true;

    }
}


private class MyDragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {

        int action = dragEvent.getAction();

        switch (action) {

            case ACTION_DROP:

                View viewBeingHold = (View) dragEvent.getLocalState();
                ViewGroup owner = (ViewGroup) viewBeingHold.getParent();

                owner.removeView(viewBeingHold);
                LinearLayout containerWhereViewWillBeDropped = (LinearLayout) view;
                viewBeingHold.setVisibility(View.VISIBLE);

                View viewAlreadyOnLayout = containerWhereViewWillBeDropped.getChildAt(0);

                if (viewAlreadyOnLayout != null) {

                    containerWhereViewWillBeDropped.removeView(viewAlreadyOnLayout);

                    containerWhereViewWillBeDropped.addView(viewBeingHold);

                    owner.addView(viewAlreadyOnLayout);

                } else if (viewAlreadyOnLayout == null) {// it means we're still on the same layout
                    owner.addView(viewBeingHold);

                }

                updateDatabaseWithTeamPosition();

                break;
        }

        return true;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM