gpt4 book ai didi

android - ViewFlipper 不翻转子 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:37 24 4
gpt4 key购买 nike

滑动时 ViewFlipper 不工作。如果我放置其他 View 而不是 ListView ,则翻转效果很好。我应该为 ListView 添加任何其他代码吗? View 鳍状肢内的 ScrollView 也不适合我:(这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vf"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ViewFlipper>

这是我的类(class)代码。

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
private ListView lv;
private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain",
"Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
"Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" };
private String[] country = { "India", "Bhutan", "Kuwait", "USA", };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
lv = (ListView) findViewById(R.id.List01);
ListView lv2 = (ListView) findViewById(R.id.List02);

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils
.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils
.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this,
R.anim.slide_right_out);

gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, city));
lv2.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, country));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View view, int position,
long id) {
// user clicked a list item, make it "selected"
Toast.makeText(getBaseContext(), "Item Clicked",
Toast.LENGTH_SHORT).show();
// selectedAdapter.setSelectedPosition(position);
}
});
lv2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View view, int position,
long id) {
// user clicked a list item, make it "selected"
Toast.makeText(getBaseContext(), "Item List2 Clicked",
Toast.LENGTH_SHORT).show();
// selectedAdapter.setSelectedPosition(position);
}
});
}

class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
} catch (Exception e) {
// nothing
}
return false;
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}

覆盖 dispatchTouchEvent 而不是 onTouchEvent 可以使 ScrollView 和手势都正常工作。

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(event);
return gestureDetector.onTouchEvent(event);
}

最佳答案

滚动没有生效,因为你已经覆盖了手势,所以,在这种情况下,你还需要手动使滚动生效。

关于android - ViewFlipper 不翻转子 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com