1

I have made an app in which I have added functionality of vertical swipe using ViewPager class. Now I need help to also add Horizontal swipe. I have seen several projects on GitHub for vertical & horizontal swipe together in same activity but could not figure out how can I integrate this with my code.

Here is how I implemented Vertical Swipe:

activity_main

<VerticalViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity

public class MainActivity extends AppCompatActivity SlidingFragment.AppData {

    List<Data> mData = new ArrayList<>();
    ScreenSlidePagerAdapter screenSlidePagerAdapter;
    VerticalViewPager verticalViewPager;
    DatabaseHelper dbHelper = new DatabaseHelper(this);
    public static int DATA_CARDS = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mains);  

        mData = dbHelper.getAll(Data.class);
        DATA_CARDS = mData.size();

        ScreenSlidePagerAdapter screenSlidePagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        VerticalViewPager verticalViewPager = (VerticalViewPager) findViewById(R.id.pager);
        verticalViewPager.setAdapter(screenSlidePagerAdapter);      
    }

    @Override
    public JSONObject getData(int position) {
        String body = "";
        String imageUrl = "";
        JSONObject jsonObject = new JSONObject();
        try {
            body = mData.get(position).getContent();
            imageUrl = mData.get(position).getImage();

            jsonObject.put("image_url", imageUrl);
            jsonObject.put("body", body);
        } catch (Exception e){
            e.printStackTrace();
        }
        return jsonObject;
    }
}

ScreenSlidePagerAdapter

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    private static final int NUM_PAGES = MainActivity.DATA_CARDS;

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        SlidingFragment slidingFragment = new SlidingFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        slidingFragment.setArguments(bundle);
        return slidingFragment;
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

VerticalViewPager

public class VerticalViewPager extends ViewPager {

    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setPageTransformer(true, new VerticalPageTransformer());]
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    private class VerticalPageTransformer implements ViewPager.PageTransformer {

        @Override
        public void transformPage(View view, float position) {

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                view.setAlpha(1);

                // Counteract the default slide transition
                view.setTranslationX(view.getWidth() * -position);

                //set Y position to swipe in from top
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    /**
     * Swaps the X and Y coordinates of your touch event.
     */
    private MotionEvent swapXY(MotionEvent ev) {
        float width = getWidth();
        float height = getHeight();

        float newX = (ev.getY() / height) * width;
        float newY = (ev.getX() / width) * height;

        ev.setLocation(newX, newY);

        return ev;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){
        boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
        swapXY(ev); // return touch coordinates to original reference frame for any child views
        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapXY(ev));
    }
}

fragment_sliding

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image"
        fresco:roundedCornerRadius="5dp"
        android:layout_width="match_parent"
        android:layout_margin="@dimen/fragment_image_margin"
        android:layout_height="200dp"
        fresco:placeholderImage="@drawable/logo" />

    <TextView
        android:id="@+id/text"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_below="@+id/image"/>
</RelativeLayout>

SlidingFragment

public class SlidingFragment extends Fragment {

    private AppData mAppData;
    private int mPosition;


    public interface AppData {
        JSONObject getData(int position);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAppData = (AppData) context;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_sliding, container, false);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        populateData();
    }

    private void populateData(){
        String body;
        String imageUrl;
        JSONObject jsonObject = mAppData.getData(getArguments().getInt("position"));
        try {
            body = jsonObject.getString("body");
            imageUrl = jsonObject.getString("image_url");
            ((TextView) getView().findViewById(R.id.text)).setText(body);

            Uri uri = Uri.parse(imageUrl);
            SimpleDraweeView draweeView = (SimpleDraweeView) getView().findViewById(R.id.image);
            draweeView.setImageURI(uri);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Any help will be appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77
  • What's the problem till now ? – Aditya Jan 05 '18 at 18:36
  • @Heisen-Berg Thank you for showing interest in my problem. I need kind of guidance to add Horizontal Swipe with Vertical Swipe as I have no idea how to do both things in the same activity. – Faisal Shaikh Jan 05 '18 at 18:39
  • here is shown how you can add touch event and detect the slide events in all sides. https://stackoverflow.com/a/48108078/4517450 – vikas kumar Jan 05 '18 at 18:43
  • @vikaskumar after detect swipe gesture, what next to do? How to two view pager in same activity? – Faisal Shaikh Jan 05 '18 at 18:51
  • im not sure about using two viewpager one for vertical and one for horizontal but if you do make sure you hide one before showing another and vice versa or you just simply use fragments instead of viewpager and when one of the direction in onSwipe simply just replace existing fragment – vikas kumar Jan 05 '18 at 18:55

0 Answers0