简体   繁体   中英

Views Navigation Using Swipe Gesture android

Based on json ArrayList size I'm creating TextView 's.

By using the class Display , made each TextView height and width to cover the entire screen.

MOTTO

Only 1 TextView should be visible on the screen. 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

So far I've tried using ViewFlipper , TextView array to enable switching between TextView .But FAILED :(

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

    E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback

  2. Array:

    E/InputEventReceiver﹕ Exception dispatching input event. -- java.lang.ArrayIndexOutOfBoundsException

EDIT

I found this Question related to ios . Searching the same for android

I'm trying to develop a app similar to SimplEye which will be used by Visually disabled people.

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.

Kindly suggest what Technique should be used. Thank you

bases on the question what i can suggest is use ViewPager

which is alternative for your MOTTO not the solutions of your issue

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

    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

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). Make your ViewPager layout_width / layout_height attrs both match_parent (define ViewPager in xml layout not via code). Also make your TextView layout_width / layout_height match_parent too thus you don't need Display class at all.

EDIT. According to latest edition TS needs to handle gesture navigation across the whole app. 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. 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.

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
  2. How to add our own gestures in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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