简体   繁体   中英

Android ListView OnTouchListener not working

I created a custom listview inside fragment. Now I want to swipe left to right. I created a listener class for that. But listener not working for me.

Here is the listener class:

public class SwipeDetector implements OnTouchListener {

public static enum Action {
    LR, // Left to Right
    RL, // Right to Left
    TB, // Top to bottom
    BT, // Bottom to Top
    None // when no action was detected
}

private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private static final int HORIZONTAL_MIN_DISTANCE = 40;
private static final int VERTICAL_MIN_DISTANCE = 80;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;

public boolean swipeDetected() {
    return mSwipeDetected != Action.None;
}

public Action getAction() {
    return mSwipeDetected;
}

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.None;
            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getX();
            upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            // horizontal swipe detection
            if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    Log.i(logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return true;
                }
                if (deltaX > 0) {
                    Log.i(logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return true;
                }
            } else 

            // vertical swipe detection
            if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
                // top or down
                if (deltaY < 0) {
                    Log.i(logTag, "Swipe Top to Bottom");
                    mSwipeDetected = Action.TB;
                    return false;
                }
                if (deltaY > 0) {
                    Log.i(logTag, "Swipe Bottom to Top");
                    mSwipeDetected = Action.BT;
                    return false;
                }
            } 
            return true;
        }
    }
    return false;
}
}

And here is the Fragment:

public class FragmentOne extends Fragment implements OnClickListener {

Button addItem;
List<String> Testing;
ArrayAdapter<String> productAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    // addChildFragOne();
    // addChildFragTwo();
    View RootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
    ListView productsList = (ListView) RootView.findViewById(R.id.listView1);

    Testing = new ArrayList<String>();
    Testing.add("Hey");
    Testing.add("Hey");
    Testing.add("Hey");
    Testing.add("Hey");
    productAdapter = new ArrayAdapter(getActivity(), R.layout.product_item, R.id.textView2, Testing);
    productsList.setAdapter(productAdapter);

    addItem = (Button) RootView.findViewById(R.id.button1);
    addItem.setOnClickListener(this);

    final SwipeDetector swipeDetector = new SwipeDetector();

    productsList.setOnTouchListener(swipeDetector);
    productsList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (swipeDetector.swipeDetected()) {
                // do the onSwipe action
                Toast.makeText(getContext(), "Text!", Toast.LENGTH_SHORT).show();
                Testing.remove(position);
                productAdapter.notifyDataSetChanged();
            } else {
                // do the onItemClick action
            }
        }
    });

    return RootView;

}


}

Why OnTouchListener not working? Please help. Thanks

your setOnItemClickListener is conflicing with OnTouchListener . In runtime you only can use one, so in your case i think you should use OnTouchListener instea of setOnItemClickListener (not set setOnItemClickListener ).

Example code you can read here use OnTouchListener instea of ItemClickListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM