Tuesday 10 September 2019

How to swap from one linear layout to another linear layout using drag and drop framework in android

I have text-view and image-view in one linear layout. I want to drag first linear layout to second linear layout in such a manner, when I drop the first linear to second linear layout, its text-view and image-view should come in first linear layout and vice versa. I have done the R&D where I have found to drag and drop layout from first to second but the second layout is not coming in first layout.

The problem is the layouts are not getting exchange. I am able to drag and drop from first to second but unable to do first to second.

Below is my fragment :

public class MoreDestinationFragment extends Fragment implements View.OnClickListener, View.OnTouchListener, View.OnDragListener {

    private OnFragmentInteractionListener mListener;
    private  LinearLayout llNews;

    public MoreDestinationFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more_destination, container, false);

        llNews = view.findViewById(R.id.ll_new);
        LinearLayout llBase = view.findViewById(R.id.ll_base);
        LinearLayout llCanteenMenu = view.findViewById(R.id.ll_canteen_menu);
        LinearLayout llTimeAccount = view.findViewById(R.id.ll_time_account);
        LinearLayout llTimeguard = view.findViewById(R.id.ll_timeguard);
        LinearLayout llFitness = view.findViewById(R.id.ll_fitness);
        LinearLayout llPayroll = view.findViewById(R.id.ll_payroll);
        LinearLayout llLinks = view.findViewById(R.id.ll_links);
        LinearLayout llSupport = view.findViewById(R.id.ll_support);
        LinearLayout llWhatWords = view.findViewById(R.id.ll_what_word);
        LinearLayout llAppRaing = view.findViewById(R.id.ll_app_rating);
        LinearLayout llServcerConfig = view.findViewById(R.id.ll_server_config);
        LinearLayout llTest = view.findViewById(R.id.ll_test);
        LinearLayout llConfiguration = view.findViewById(R.id.ll_configuration);

        llNews.setOnClickListener(this);
        llNews.setOnTouchListener(this);
        llNews.setOnDragListener(this);
        llBase.setOnDragListener(this);
        llCanteenMenu.setOnClickListener(this);
        llTimeAccount.setOnClickListener(this);
        llTimeguard.setOnClickListener(this);
        llFitness.setOnClickListener(this);
        llPayroll.setOnClickListener(this);
        llLinks.setOnClickListener(this);
        llSupport.setOnClickListener(this);
        llWhatWords.setOnClickListener(this);
        llAppRaing.setOnClickListener(this);
        llServcerConfig.setOnClickListener(this);
        llTest.setOnClickListener(this);
        llConfiguration.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_new:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_newsDestinationFragment);
                break;

            case R.id.ll_canteen_menu:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_canteenMenuDestinationFragment);
                break;

            case R.id.ll_time_account:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_timeAccountFragment);
                break;

            case R.id.ll_timeguard:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_timeGuardDestinationFragment);
                break;

            case R.id.ll_fitness:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_fitnessDestinationFragment);
                break;

            case R.id.ll_payroll:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_payRollDestinationFragment);
                break;

            case R.id.ll_links:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_linksDestinationFragment2);
                break;

            case R.id.ll_support:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_supportDestinationFragment);
                break;

            case R.id.ll_what_word:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_whatWordsDestinationFragment);
                break;

            case R.id.ll_app_rating:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_appRatingDestinationFragment);
                break;

            case R.id.ll_server_config:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_serverConfigDestinationFragment);
                break;

            case R.id.ll_test:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_testDestinationFragment);
                break;

            case R.id.ll_configuration:
                Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_configurationDestinationFragment);
                break;
        }
    }

    @Override
    public boolean onDrag(View layoutview, DragEvent dragevent) {
        int action = dragevent.getAction();
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.d("xxxxxxx", "Drag event started");
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                Log.d("xxxxxxx", "Drag event entered into "+layoutview.toString());
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                Log.d("xxxxxxx", "Drag event exited from "+layoutview.toString());
                break;
            case DragEvent.ACTION_DROP:
                Log.d("xxxxxxx", "Dropped");
                View view = (View) dragevent.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                LinearLayout container = (LinearLayout) layoutview;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                Log.d("xxxxxxx", "Drag ended");
                break;
            default:
                break;
        }
        return true;    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        }
        else {
            return false;
        }    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
} 

Below is my view :

enter image description here

Please help me out and please advice the solution. Thanks.



from How to swap from one linear layout to another linear layout using drag and drop framework in android

No comments:

Post a Comment