掉落的物品在掉落后消失

时间:2018-07-16 16:02:40

标签: java android

我创建了一个RV,其中每个项目都有一个onClicklistener来将其拖放到屏幕上的任何位置。

现在,我已经能够设置OnClickListener,创建拖动项,但是当我释放该项时,它不会停留在放置它的位置,而会消失。

我花了几天时间尝试解决此问题,然后查看Stackover流程。但是到目前为止,还没有运气。

这是RecyclerView的onBind,用于设置onClick和拖动:

public void onBindViewHolder(final FootballPitchMainAdapter.ViewHolder holder, int position) {
        holder.rvPlayerNameTop.setText("testing");

        holder.linearLayoutFull.setOnTouchListener(new 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;
                }
            }
        });
    }

这可以按预期工作(无论如何,我可以单击并拖动每个项目)

我将onClickListener设置为:

    pitchRelativeLayout = findViewById(R.id.mainPitch);
    pitchRelativeLayout.setOnDragListener(new MyDragListener());
 public class MyDragListener implements View.OnDragListener {

        int x_cord;
        int y_cord;

        @Override
        public boolean onDrag(View v, DragEvent event) {

            switch(event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    rootLayout = new FrameLayout.LayoutParams(pitchRelativeLayout.getWidth(), pitchRelativeLayout.getHeight());
                    rootLayout.setMargins(300, 0, 300, 0);
                    break;

                case DragEvent.ACTION_DRAG_ENTERED:
                    break;

                case DragEvent.ACTION_DRAG_EXITED :
                    break;

                case DragEvent.ACTION_DRAG_LOCATION  :
                    //x_cord = (int) event.getX();
                    //y_cord = (int) event.getY();
                    break;

                case DragEvent.ACTION_DRAG_ENDED   :
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    rootLayout.leftMargin = x_cord;
                    rootLayout.topMargin = y_cord;
                    v.setLayoutParams(rootLayout);
                    break;

                case DragEvent.ACTION_DROP:
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    RelativeLayout container = (RelativeLayout) v;
                    container.addView(view);
                    view.setVisibility(View.VISIBLE);

                    break;
                default: break;
            }
            return true;
        }
    }

这是我的布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linerLayoutPlayerFull"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

        <ImageView
            android:id="@+id/ivPlayerTop"
            android:layout_width="88dp"
            android:layout_height="53dp"
            android:src="@drawable/ic_football_top" />
        <TextView
            android:id="@+id/playerNameTop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
</LinearLayout>

我不确定我需要做什么,或者我缺少什么才能让被拖动的项目移动到被拖动的位置。

0 个答案:

没有答案