Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ViewFlipper未翻转_Android_Viewflipper - Fatal编程技术网

Android ViewFlipper未翻转

Android ViewFlipper未翻转,android,viewflipper,Android,Viewflipper,我有一个可视翻转器,它应该对投掷动作做出反应,但它没有 活动 @Override public void onCreate(Bundle savedInstanceState) { ... listView = this.getListView(); detector = new GestureDetector(this, new FlingGestureListener(listView)); ... } 弗林格斯特雷斯滕纳 public boolean on

我有一个可视翻转器,它应该对投掷动作做出反应,但它没有

活动

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    listView = this.getListView();
    detector = new GestureDetector(this, new FlingGestureListener(listView));
    ...
}
弗林格斯特雷斯滕纳

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int pos = source.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY()));
    View v = source.getChildAt(pos - source.getFirstVisiblePosition());
    System.out.println("fling: x=" + velocityX + " y=" + velocityY);
    try {
        IFlingable flingable = (IFlingable) v;
        if(velocityY > -200 && velocityY < 200) {
            if(velocityX < 0)
                flingable.fling(IFlingable.RIGHT_TO_LEFT);
            else if(velocityX > 0)
                flingable.fling(IFlingable.LEFT_TO_RIGHT);
        }
    } catch(Exception e) {}
    return false;
}
布局

<ViewFlipper android:id="@+id/viewSwitcher"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" android:layout_weight="1"     
    android:inAnimation="@anim/slide_in_left" 
    android:outAnimation="@anim/slide_out_right">
        <LinearLayout android:layout_height="wrap_content" 
           android:layout_width="match_parent" android:orientation="vertical">
             ...
        </LinearLayout>
      ...
    </ViewFlipper>
我获得了正确的日志消息,因此可以执行ViewFlipper上的showNext(),但不会更改其在gui上的视图。我错过什么了吗?我有另一个布局,用一个ViewSwitcher而不是Flipper,这个布局很有效

编辑:

以下是缺少的类:

public class GUIHelper {
... 

public static void setAnimationSlideLeftToRight(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_left);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_right);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

public static void setAnimationSlideRightToLeft(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_right);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_left);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

...
}

public interface IFlingable {

    public static final int LEFT_TO_RIGHT = 0;
    public static final int RIGHT_TO_LEFT = 1;

    public void fling(int direction, boolean fling);

}

您好,这是listView上运行的滑动代码,就像您的xml一样,我在ViewFlipper中放置了一个具有父LinearLayout的listView

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;
    }
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.GestureDetector;
导入android.view.GestureDetector.SimpleOnGestureListener;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.widget.ViewFlipper;
导入android.widget.AdapterView.OnItemClickListener;
公共类MainActivity扩展了活动{
专用静态最终整数滑动距离=120;
专用静态最终整数滑动路径=250;
专用静态最终整数滑动\u阈值\u速度=200;
私人手势检测器;
View.OnTouchListener手势Listener;
私人动画幻灯片;
私人动画幻灯片;
私人动画幻灯片;
私人动画幻灯片;
私有ViewFlipper ViewFlipper;
私有ListView lv;
私有字符串[]城市={“印多尔”、“博帕尔”、“哈戈内”、“乌贾因”,
“纳西克”、“浦那”、“德里”、“孟买”、“诺伊达”、“海得拉巴”,
“班格洛尔”、“阿杰梅尔”、“果阿”、“斋浦尔”、“那格浦尔”等;
私有字符串[]国家={“印度”、“不丹”、“科威特”、“美国”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、主布局);
lv=(ListView)findViewById(R.id.List01);
ListView lv2=(ListView)findViewById(R.id.List02);
viewFlipper=(viewFlipper)findViewById(R.id.flipper);
slideLeftIn=AnimationUtils.loadAnimation(这是R.anim.slide\u left\u in);
slideLeftOut=AnimationUtils
.loadAnimation(这是R.anim.幻灯片左滑出);
slideRightIn=AnimationUtils
.loadAnimation(此为R.anim.幻灯片右滑入);
slideRightOut=AnimationUtils.loadAnimation(此,
R.anim.向右滑出);
gestureDetector=新的gestureDetector(新的MyGestureDetector());
gestureListener=新建视图。OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
if(手势检测器onTouchEvent(事件)){
返回true;
}
返回false;
}
};
lv.setAdapter(新阵列适配器),
android.R.layout.simple_list_item_1,city));
lv2.setAdapter(新阵列适配器)(此,
android.R.layout.simple_list_item_1,country));
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(适配器视图arg0、视图视图、内部位置、,
长id){
//用户单击列表项,将其设置为“选中”
Toast.makeText(getBaseContext(),“单击项”,
吐司。长度(短)。show();
//selectedAdapter.设置SelectedPosition(位置);
}
});
lv2.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(适配器视图arg0、视图视图、内部位置、,
长id){
//用户单击列表项,将其设置为“选中”
Toast.makeText(getBaseContext(),“已单击项目列表2”,
吐司。长度(短)。show();
//selectedAdapter.设置SelectedPosition(位置);
}
});
}
类MyGestureDetector扩展了SimpleOnGestureListener{
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、浮点速度X、,
浮动速度y){
试一试{
if(Math.abs(e1.getY()-e2.getY())>swip\u MAX\u OFF\u路径)
返回false;
//从右向左滑动
如果(e1.getX()-e2.getX()>滑动最小距离
&&Math.abs(velocityX)>滑动阈值(速度){
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
}else if(e2.getX()-e1.getX()>滑动最小距离
&&Math.abs(velocityX)>滑动阈值(速度){
viewFlipper.setInAnimation(滑动照明);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
}捕获(例外e){
//没什么
}
返回false;
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(手势检测器onTouchEvent(事件))
返回true;
其他的
返回false;
}
}
包括使用的布局(主布局)

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not Click Just swipe on remaining area" />
    </LinearLayout>

    <ViewFlipper
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ViewFlipper01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/List01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>

这是我使用的xml代码

  <ViewFlipper 
    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"
        ></ListView>
    </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"
        ></ListView>
    </LinearLayout>
</ViewFlipper>

这很好:)滚动视图和翻转器都很好。

我发现了错误。我没有在类中的某个点保存上下文。上面的代码很好用。你从哪里得到“IFlingable”?在Android API中看不到它。还有,“源”是什么类型的对象?还有,“GUIHelper”来自的idk。您在后台做了一些事情,使得这个示例有点困难
 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not Click Just swipe on remaining area" />
    </LinearLayout>

    <ViewFlipper
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ViewFlipper01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/List01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
  <ViewFlipper 
    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"
        ></ListView>
    </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"
        ></ListView>
    </LinearLayout>
</ViewFlipper>
@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }