如何使用垂直滑动添加水平滑动?

时间:2018-01-05 18:34:51

标签: android android-viewpager android-adapter android-pageradapter android-swipe

我制作了一个应用程序,其中我使用ViewPager类添加了垂直滑动功能。现在我还需要帮助来添加水平滑动。我在GitHub上看过几个关于垂直和放大的项目。水平滑动在同一个活动中,但无法弄清楚如何将其与我的代码集成。

以下是我实施垂直滑动的方法:

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();
        }
    }
}

任何帮助将不胜感激。

0 个答案:

没有答案