![](/img/trans.png)
[英]Android CardView with ListView inside - onTouchListener on CardView not working
[英]Android ListView OnTouchListener not working
我在片段中創建了一個自定義listview。 現在,我想從左向右滑動。 我為此創建了一個偵聽器類。 但是聽眾不為我工作。
這是偵聽器類:
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不起作用? 請幫忙。 謝謝
您setOnItemClickListener
與conflicing OnTouchListener
。 在運行時,您只能使用一個,因此在您的情況下,我認為您應該使用setOnItemClickListener的OnTouchListener插件(而不是set setOnItemClickListener)。
您可以在此處閱讀的示例代碼使用ItemClickListener的OnTouchListener instea
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.