I have a ListView, and when you tap on an item, I want to check whether the item was clicked on on the left or the right half. This is the code I have:
package com.testapp.toroco;
import android.support.v4.app.ListFragment;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentA extends ListFragment implements OnClickListener {
int width;
public void onCreate(Bundle b) {
super.onCreate(b);
getSize();
}
public void getSize(){
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] itemlist = {"A","b","C","d","E","f"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),R.layout.layoutrow,R.id.name,itemlist);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
public boolean dispatchTouchEvent(MotionEvent event) {
int actionX = (int) event.getX();
int middlePoint = width/2 ;
if(actionX >middlePoint ){
Log.d("Touch", "Right");
}else if (actionX <middlePoint ){
Log.d("Touch", "Left");
}
return false;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
I am aware that I haven't properly added the listener and that The onListItemClick and dispatchTouchEvent are currently two seperate events. But unfortunately I have now idea how I can accomplish that. Is there a better way to find out on which half it was clicked on? If not, on which View do I have to register the ClickListener and how can I make one Event (onListItemClick and dispatchTouchEvent) check the outcome of the other?
Thank you very much for your help!