问题描述
|
我正在制作一个Android应用程序,在其中放置了10张图像,并在其上添加了触摸功能,但是每当到达10张图像时,它仍在工作并返回到1张图像。
我想在第10张图像或最后一张图像上停止ontouch功能。
有人知道这个功能吗?
这是Java代码:
public class Activity1 extends Activity implements OnTouchListener{
float downXValue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set main.XML as the layout for this Activity
setContentView(R.layout.main);
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setonTouchListener((OnTouchListener) this);
// Add a few countries to the spinner
Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,new String[] { \"Canada\",\"USA\" });
spinnerCountries.setAdapter(countryArrayAdapter);
}
public boolean onTouch(View arg0,MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user\'s finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
// Flip!
vf.showPrevIoUs();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
// Flip!
vf.showNext();
}
break;
}
}
// if you return false,these actions will not be recorded
return true;
}
}
这是xml代码:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:id=\"@+id/layout_main\"
>
<ViewFlipper android:id=\"@+id/details\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
<ImageView android:id=\"@+id/ImageView01\" android:background=\"@drawable/eea\"
android:layout_x=\"1dip\" android:layout_y=\"1dip\"
android:layout_height=\"fill_parent\" android:layout_width=\"fill_parent\"></ImageView>
</LinearLayout>
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
<ImageView android:id=\"@+id/ImageView01\" android:background=\"@drawable/ef\"
android:layout_x=\"1dip\" android:layout_y=\"1dip\"
android:layout_height=\"fill_parent\" android:layout_width=\"fill_parent\"></ImageView>
</LinearLayout>
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
<ImageView android:id=\"@+id/ImageView01\" android:background=\"@drawable/eg\"
android:layout_x=\"1dip\" android:layout_y=\"1dip\"
android:layout_height=\"fill_parent\" android:layout_width=\"fill_parent\"></ImageView>
</LinearLayout>
<LinearLayout
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
<ImageView android:id=\"@+id/ImageView01\" android:background=\"@drawable/eh\"
android:layout_x=\"1dip\" android:layout_y=\"1dip\"
android:layout_height=\"fill_parent\" android:layout_width=\"fill_parent\"></ImageView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
我在代码中使用了View脚蹼,然后在上面添加了图像。
解决方法
@user552027 i also had the same problem in my application i have got solution for this you can check this link
如何在Android的Viewflipper中控制图片?
, 在第十张图片的onTouch()
方法中,setFocussable(\"false\");
和setFocussableInTouchMode(\"false\")
用于所有其他图像..我猜是唯一的方法..