美文网首页
通过ViewPager左右动态的切换数据

通过ViewPager左右动态的切换数据

作者: 翻滚吧王咸鱼 | 来源:发表于2018-05-21 20:43 被阅读0次

1.原型图:


QQ图片20180515203352.png

2.需求说明:
用户可以滑动查看每日的订单情况;滑动至没有数据时toast提示“没有更多的数据”;滑动后需要更新下面的列表跟日历里面的数据.


QQ图片20180515204319.png

先来首先是 XML , 这里面我这写了上拉加载下拉刷新的控件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white"
        android:padding="@dimen/common_margin_3">

        <ImageView
            android:id="@+id/image_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/common_margin_5"
            android:src="@mipmap/ic_back" />

        <TextView
            android:id="@+id/tv_member_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/common_height_10"
            android:layout_weight="1"
            android:text="订单管理"
            android:textColor="@color/color_text_000000"
            android:textSize="@dimen/text_size_17"
          />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/common_margin_1"
        android:background="#E6E6E6"/>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/coupon_refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:srlAccentColor="@color/white"
        app:srlFooterHeight="@dimen/common_height_40"
        app:srlHeaderHeight="@dimen/common_height_40">

        <!-- 头部 组件-->
        <!--  app:srlClassicsSpinnerStyle="FixedBehind"  -->
        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/Business_record_Recyiew"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:overScrollMode="never"
            android:scrollbars="vertical">

        </android.support.v7.widget.RecyclerView>
        <!-- 底部组件 -->
        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>


</LinearLayout>
  1. 我项目里使用用万能适配器 下拉刷新
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34
  //下拉刷新
    compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'

    //没有使用特殊Header,可以不添加次依赖
    compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5'

3.头布局的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    android:layout_height="wrap_content">

    <me.crosswall.lib.coverflow.core.PagerContainer
        android:id="@+id/pager_container"
        android:layout_width="match_parent"
        android:layout_height="165dp"
        android:layout_marginLeft="@dimen/common_margin_3"
        android:layout_marginRight="@dimen/common_margin_3"
        android:background="@color/white">

        <android.support.v4.view.ViewPager
            android:id="@+id/overlap_pager"
            android:layout_width="match_parent"
          android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </me.crosswall.lib.coverflow.core.PagerContainer>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/common_margin_3">

        <LinearLayout
            android:layout_width="@dimen/common_margin_0"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_business_Settlement"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="15000.00元"
                android:textColor="@color/black_333333"
                android:textSize="@dimen/text_size_17"
                android:textStyle="bold" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="@dimen/common_margin_3"
                android:text="结算中收益"
                android:textColor="#666666"
                android:textSize="@dimen/text_size_14" />


        </LinearLayout>


        <LinearLayout

            android:layout_width="@dimen/common_margin_0"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">


            <TextView
                android:id="@+id/tv_business_Profit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="15000.00元"
                android:textColor="@color/black_333333"
                android:textSize="@dimen/text_size_17"
                android:textStyle="bold" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="@dimen/common_margin_3"
                android:text="累计收益"
                android:textColor="#666666"
                android:textSize="@dimen/text_size_14" />


        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="#F2F2F2"/>

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

        <View
            android:layout_width="0dp"
            android:layout_height="@dimen/common_margin_1"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/common_height_40"
            android:layout_weight="1"
            android:background="#E6E6E6" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/common_margin_5"
            android:layout_marginRight="@dimen/common_margin_5"
            android:text="交易记录"
            android:textColor="@color/black_333333"
            android:textSize="@dimen/text_size_14" />

        <View
            android:layout_width="0dp"
            android:layout_height="@dimen/common_margin_1"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="@dimen/common_height_40"
            android:layout_weight="1"
            android:background="#E6E6E6"/>
    </LinearLayout>

</LinearLayout>
  1. 添加头布局的代码
    specHeaderView = LayoutInflater.from(this).inflate(R.layout.item_business_header, null);//添加头布局
        PagerContainer container =  specHeaderView.findViewById(R.id.pager_container); //查找控件

这是主代码

private boolean isScrolled = false; //是否第一次加载
    boolean isRight = false;// 向右

    /**
     * @param view
     */
    @SuppressLint("ClickableViewAccessibility")
    private void init(View view) {
        dialog = new ProgressDialog(getActivity());
        dialog.setCanceledOnTouchOutside(false);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        viewPager.setOnTouchListener(new OnTouchListener() {
            float x = 0;
            float distance;// 移动距离

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                try {

                    int eventAction = arg1.getAction();
                    switch (eventAction) {
                    case MotionEvent.ACTION_DOWN:// 按下时
                        x = arg1.getX();// 记录第一次按下位置
                        isScrolled = false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // distance = arg1.getX() - x;// 移动距离
                        break;
                    case MotionEvent.ACTION_UP:
                        // Toast.makeText(getActivity(),
                        // "arg1" + arg1.getX() + "x" + x, 1).show();
                        distance = arg1.getX() - x;// 移动距离
                        if (Math.abs(distance) > 170) {// 判断达到滑动标准
                            isScrolled = true;
                        } else {
                            isScrolled = false;
                        }
                        if (distance > 0) {// 向左滑动
                            // Toast.makeText(getActivity(), "向左滑动" + distance,
                            // 1)
                            // .show();
                            isRight = false;
                        } else {// 向右滑动
                            // Toast.makeText(getActivity(), "向右滑动", 1).show();
                            isRight = true;
                        }
                        break;
                    default:
                        break;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return false;
            }
        });
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // Toast.makeText(getBaseContext(), "onPageSelected" + arg0 +
                // "",
                // 1).show();
            }

            /*
             * arg0 :当前页面,及你点击滑动的页面
             * 
             * arg1:当前页面偏移的百分比
             * 
             * arg2:当前页面偏移的像素位置
             * 
             * (int, float, int)
             */
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            boolean isLast = true;

            @Override
            public void onPageScrollStateChanged(int i) {
                if (isScrolled) {
                    if (i == 2) {
                        isLast = false;
                    } else if (i == 0 && isLast) {// 最左边加载前一天的数据
                        int position = viewPager.getCurrentItem();
                        if (position == 0) {// 向左滑动
                            if (isRight && lists.size() == 1) {// 判断第一次滑动是否向右
                                Toast.makeText(
                                        getActivity(),
                                        position + "到右边、有" + lists.size()
                                                + "条数据", 1).show();
//                              Update update = new Update();
//                              update.execute("页码:" + page++, "1");
                            } else {
                                Toast.makeText(
                                        getActivity(),
                                        position + "到左边了、有" + lists.size()
                                                + "条数据", 1).show();
                                Update update = new Update();
                                update.execute("页码:-" + fast++, "0");
                            }
                        } else if (position == lists.size() - 1) {// 向右滑动
                            Toast.makeText(getActivity(),
                                    position + "到右边、有" + lists.size() + "条数据",
                                    1).show();
                            Update update = new Update();
                            update.execute("页码:" + page++, "1");
                        }
                    } else {
                        isLast = true;
                    }
                }
            }
        });
        lists = new ArrayList<Map<String, String>>();
        for (; page <= 1; page++) {
            map = new HashMap<String, String>();
            map.put("index", "页码:" + page);
            lists.add(map);
        }
        adapter = new MyPagerAdapter(getActivity(), lists);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(page + fast);
        // 默认先加载两页数据
        Update update = new Update();
        // update.execute("页码:-" + fast++, "0");
    }

    int max;

    class Update extends BaseAsyncTask {
        private String date;
        private String type;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected Integer doInBackground(String... params) {
            int resultCode = -1;
            date = params[0];
            type = params[1];
            if (type.equals("0")) {// 左边
                Log.e("--44---", "左边: "+num+","+ date);
                map = new HashMap<String, String>();
                map.put("index", date);
                lists.add(0, map);
                resultCode = 1;
            } else if (type.equals("1")) {// 右边
                Log.e("--44---", "左边: "+num+","+ date);
                map = new HashMap<String, String>();
                map.put("index", date);
                lists.add(map);
                resultCode = 2;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return resultCode;
        }

        @Override
        protected void onPostExecute(Integer resultCode) {
            super.onPostExecute(resultCode);
            max = page + fast;
            switch (resultCode) {
            case 1:
                if (lists.size() > num) {
                    lists.remove(num);
                    page--;
                }
                viewPager.setAdapter(adapter);
                dialog.dismiss();
                Log.e("-----", "左边: "+num+","+ max);
                break;
            case 2:
                Log.e("-----", "右边: "+num+","+ max);
                if (lists.size() > num) {
                    lists.remove(0);
                    fast--;
                    viewPager.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }
                viewPager.setCurrentItem(max);
                dialog.dismiss();
                break;

            default:
                break;
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

相关文章

网友评论

      本文标题:通过ViewPager左右动态的切换数据

      本文链接:https://www.haomeiwen.com/subject/ewwydftx.html