0

我想要做的是当用户按下 listView 中的某个位置时,我想看看他们是在扔视图(扔掉它)还是只是点击它(调用 onClickListener())

我已经为我的课程设置了所有内容,但我的问题在于我无法在 listView 中传递 MotionEvent,或者我只是不知道如何传递。

目前这就是我所拥有的。

有没有办法在不覆盖 dispatchTouchEvents() 的情况下传递 MotionEvent?

Main_screen_listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, final int position,
                    long arg3) {
                    //go to assignments or flashcards.
                    onTouchHandler myOnTouch = new onTouchHandler(Main_screen.this, position, classList);
                    //Right here is the problem!!!!!!!! I need to pass something other than null

                    MotionEvent ev = null;
                    listAdapter.notifyDataSetChanged();
                    classList = myOnTouch.motionHandler(ev, position, view);


                    AlertDialog.Builder alertBox = new AlertDialog.Builder(Main_screen.this);
                    alertBox.setTitle("Options")
                    .setPositiveButton("Assignments", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent currentAssignments = new Intent(Main_screen.this, assignments.Assignments.class);
                            currentAssignments.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            currentAssignments.putExtra("position", position);
                            startActivity(currentAssignments);
                        }
                    })
                    .setNegativeButton("FlashCards", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent currentFlashCards = new Intent(Main_screen.this, flashCards.FlashCard.class);
                            currentFlashCards.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            currentFlashCards.putExtra("position", position);
                            startActivity(currentFlashCards);
                        }
                    })
                    .show();
            }
        });

这是我的 onTouchHandler 类

 private class onTouchHandler extends View {

        private ArrayList<String> transferredList;
        public ArrayList<String> removeListItem(View rowView, final int position) {
            final Animation animation = AnimationUtils.loadAnimation(
                    Main_screen.this, android.R.anim.slide_out_right);
                rowView.startAnimation(animation);
                Handler handle = new Handler();
                handle.postDelayed(new Runnable() {
                    @SuppressLint({ "NewApi", "NewApi" })
                    @Override
                    public void run() {
                        transferredList.remove(position);
                        animation.cancel();
                    }
                }, 1000);
                return transferredList;
        }

        public ArrayList<String> motionHandler(MotionEvent event, int position, View rowView) {
            float startY;
            float stopX;
            float stopY;
            float finalX;
            float finalY;


            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                startX = event.getX();
                startY = event.getY();
                Toast.makeText(Main_screen.this, String.valueOf(startX), Toast.LENGTH_SHORT).show();
            }
            else if (event.getAction() == MotionEvent.ACTION_UP) {
                stopX = event.getX();
                stopY = event.getY();
                finalX = stopX - startX;
                if (finalX > 200) {
                    Toast.makeText(Main_screen.this, String.valueOf(finalX), Toast.LENGTH_SHORT).show();
                    transferredList = removeListItem(rowView, position);
                }
            }

            return transferredList; 
        }

        public onTouchHandler(Context context, ArrayList<String> mainListView) {
            super(context);
            transferredList = mainListView;
        }

    }
4

1 回答 1

0

更好的解决方案是覆盖getView()您的Adapter,然后将onTouchListener()返回的 View 设置为您想要的。

于 2013-01-31T09:14:03.390 回答