简体   繁体   English

使用滑动手势Android进行视图导航

[英]Views Navigation Using Swipe Gesture android

Based on json ArrayList size I'm creating TextView 's. 基于json ArrayList大小,我正在创建TextView

By using the class Display , made each TextView height and width to cover the entire screen. 通过使用Display类,使每个TextView高度和宽度都覆盖整个屏幕。

MOTTO 座右铭

Only 1 TextView should be visible on the screen. 屏幕上仅应显示1个TextView By swiping it should move to next view which will again occupy the entire screen. 通过滑动,它应该移至下一个视图,该视图将再次占据整个屏幕。

Swipe down and Swipe up will move the screens ie, views... swipe left and swipe right should do some other tasks,such as changing activity 向下滑动和向上滑动将移动屏幕,即视图...向左滑动和向右滑动应执行其他一些任务,例如更改活动

Swipe is enabled by using GestureDetector.SimpleOnGestureListener 通过使用GestureDetector.SimpleOnGestureListener启用滑动

So far I've tried using ViewFlipper , TextView array to enable switching between TextView .But FAILED :( 到目前为止,我已经尝试使用ViewFlipperTextView数组启用TextView之间的切换。但是失败:(

Code snippet: 程式码片段:

        for(int i=0;i<name.size();i++)
        {
            text = new TextView(MainActivity.this);
            text.setText(name.get(i));
            text.setId(i);
            text.setTextColor(Color.parseColor("#000000")); 
            text.setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
            text.setGravity(Gravity.CENTER);
            text.setTextSize(40);
            text.setClickable(true);
            vf.addView(text);

           /* 
           //I've tried the following code while using TextView array
            myTextViews[i] = text;
            myTextViews[i].setId(i);
            myTextViews[i].setTextColor(Color.BLACK);
            myTextViews[i].setText(name.get(i));
            myTextViews[i].setGravity(Gravity.CENTER);
            myTextViews[i].setTextSize(40);
            myTextViews[i].setLayoutParams(new LinearLayout.LayoutParams(realWidth, realHeight));
            myTextViews[i].onWindowFocusChanged(false);
            LL.addView(myTextViews[i]);
*/

            View lines = new View(getApplicationContext());
            lines.setBackgroundColor(Color.parseColor("#000000"));
            lines.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
            vf.addView(lines);

            final int finalI = i;


            text.setOnTouchListener(new MainActivity()
            {
                @Override
                public void onSwipeLeft()
                { 
                    if (vf.getDisplayedChild() == 0)
                        Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
                    else
                        vf.showNext();
                }

                @Override
                public void onSwipeRight()
                {
                    if (vf.getDisplayedChild() == 0)
                        Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
                    else
                        vf.showPrevious();
                }
            });
        }

Errors: 错误:

  1. While using ViewFlipper 使用ViewFlipper

    E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback

  2. Array: 阵:

    E/InputEventReceiver﹕ Exception dispatching input event. E / InputEventReceiver:异常调度输入事件。 -- java.lang.ArrayIndexOutOfBoundsException -java.lang.ArrayIndexOutOfBoundsException

EDIT 编辑

I found this Question related to ios . 我发现这个问题ios有关。 Searching the same for android 搜索相同的android

I'm trying to develop a app similar to SimplEye which will be used by Visually disabled people. 我正在尝试开发类似于SimplEye的应用程序,该应用程序将被视障人士使用。

For that, I need to control the swipes on the screen so that entire app could be handled only through the help of swipes. 为此,我需要控制屏幕上的滑动,以便只能通过滑动来处理整个应用程序。

ViewPager , ViewFlipper , SimpleOnGestureListener are not matching the requirement. ViewPager,ViewFlipper,SimpleOnGestureListener不符合要求。

Kindly suggest what Technique should be used. 请建议应使用哪种技术。 Thank you 谢谢

bases on the question what i can suggest is use ViewPager 基于这个问题,我可以建议使用ViewPager

which is alternative for your MOTTO not the solutions of your issue 这是您的MOTTO的替代选择,而不是问题的解决方案

activity_main.xml activity_main.xml中

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

ViewPagerActivity ViewPagerActivity

    public class ViewPagerActivity extends Activity {
                String text[] = {"A", "B",
                        "C", "D",
                        "E", "F",
                        "G", "H"};

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    MyPagerAdapter adapter = new MyPagerAdapter(this, text);
                    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
                    myPager.setAdapter(adapter);
                    myPager.setCurrentItem(0);
//set Page Change Listner. to get callback on page changed or swiped
    myPager .setOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    Log.e("Page Changed ", " YES ");
                    /// here you can check & perform on changed
                    Log.e("Current TextView Text ", text[position]);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

                }
            }

MyPagerAdapter MyPagerAdapter

public class MyPagerAdapter extends PagerAdapter {

        Activity activity;
        int txtarray[];

        public MyPagerAdapter(Activity act, int[] imgArra) {
            txtarray = imgArra;
            activity = act;
        }

        public int getCount() {
            return txtarray.length;
        }

        public Object instantiateItem(View collection, int position) {
            TextView view = new TextView(activity);
            view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            view.setText(txtarray[position]);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }

Use ViewPager for sliding the views (only by swipe gesture; if you need swipe during time then ViewFlipper is better approach). 使用ViewPager滑动视图(仅通过滑动手势即可;如果需要在时间上滑动,则ViewFlipper是更好的方法)。 Make your ViewPager layout_width / layout_height attrs both match_parent (define ViewPager in xml layout not via code). 使您的ViewPager layout_width / layout_height ViewPager都为match_parent (不通过代码在xml布局中定义ViewPager )。 Also make your TextView layout_width / layout_height match_parent too thus you don't need Display class at all. 另外,也使TextView layout_width / layout_height match_parent因此您根本不需要Display类。

EDIT. 编辑。 According to latest edition TS needs to handle gesture navigation across the whole app. 根据最新版本,TS需要处理整个应用程序中的手势导航。 I suggest the following: 我建议以下内容:

  1. try to not use any widgets which handle gestures (click) by themselves (button, checkbox etc.). 尽量不要使用任何单独处理手势(单击)(按钮,复选框等)的小部件。 Use only uninteractable Views and implement onTouchEvent method inside your Activity which will receive all touch events in this case. 仅使用不可交互的视图,并在Activity实现onTouchEvent方法,在这种情况下,它将接收所有触摸事件。 In that method you can add any gesture handling you want. 在该方法中,您可以添加所需的任何手势处理。
  2. You can create your own ViewGroup implementation and override there onInterceptTouchEvent / onTouchEvent methods and perform there any gesture handling manually. 您可以创建自己的ViewGroup实现,并在那里覆盖onInterceptTouchEvent / onTouchEvent方法,并在那里手动执行任何手势处理。

In both cases you need to create all gesture detection logic by yourself. 在这两种情况下,您都需要自己创建所有手势检测逻辑。

These links may be helpful 这些链接可能会有所帮助

  1. Creating a simple Gesture Application in Android 在Android中创建一个简单的Gesture应用程序
  2. How to add our own gestures in android? 如何在android中添加自己的手势?
  3. Detecting Common Gestures 检测常见手势

Never did it but the first link seems to be the most useful. 从来没有,但是第一个链接似乎是最有用的。 It says that you can firstly create some kind of gesture description and then gesture API can check any gesture performed for match to that description. 它说您可以首先创建某种手势描述,然后手势API可以检查所执行的任何手势以匹配该描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM