我在片段中创建了自定义列表视图。现在我想从左到右滑动。我为此创建了一个监听器类。但是听众不为我工作。Android ListView OnTouchListener不能正常工作
下面是监听器类:
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;
}
}
这里是片段:
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;
}
}
为什么OnTouchListener不工作?请帮忙。谢谢
也许onTouch与OnItemClickListener conflicing。所以你可以尝试只使用onTouch来持有刷卡和物品点击 – GiapLee