gpt4 book ai didi

java - 如何使用 DragEvent 将图像放置在线性布局中

转载 作者:行者123 更新时间:2023-11-30 05:05:16 26 4
gpt4 key购买 nike

我正在尝试实现拖动拼图,我将一个图像移动到 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);
}
}

这是我的布局
normal image

我不希望这样的事情发生
two images in the same LinearLayout

每个 LinearLayout 只有一个,将图像移到 LinearLayouts 之外会使图像消失

最佳答案

我之前遇到过同样的问题this一段代码可能会帮助你。

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

 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;
}
}

关于java - 如何使用 DragEvent 将图像放置在线性布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com