What I want to do is when the user presses on a position in the listView I want to see if they are flinging the view (throwing it away) or if they are just clicking it (calling the onClickListener())
I have everything set up for my classes but my problem lies in the fact that I can’t pass a MotionEvent inside of a listView, or I just don’t know how to.
Currently this is what I’ve got.
Is there a way to pass a MotionEvent without overriding dispatchTouchEvents()?
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();
}
});
Here is my onTouchHandler class
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;
}
}
A better solution is to override
getView()in yourAdapterand then set theonTouchListener()of your returned View to what you want.