Fragment滑动切换Bar动画

来源:互联网 发布:求生之路2不同网络 编辑:程序博客网 时间:2024/05/22 03:34

布局

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <!--我的订单-->    <include layout="@layout/base_title_1" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/text_white"        android:orientation="horizontal">        <RelativeLayout            android:id="@+id/rl1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1">            <TextView                android:id="@+id/tv_1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                android:layout_marginTop="20dp"                android:text="待支付"                android:textColor="@color/Theme_color"                android:textSize="15sp" />        </RelativeLayout>        <RelativeLayout            android:id="@+id/rl2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1">            <TextView                android:id="@+id/tv_2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                android:layout_marginTop="20dp"                android:text="运送中"                android:textColor="@color/Text_black"                android:textSize="15sp" />        </RelativeLayout>        <RelativeLayout            android:id="@+id/rl3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1">            <TextView                android:id="@+id/tv_3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                android:layout_marginTop="20dp"                android:text="已完成"                android:textColor="@color/Text_black"                android:textSize="15sp" />                  </RelativeLayout>    </LinearLayout>    <ImageView        android:id="@+id/id_tab_line_iv"        android:layout_width="50dp"        android:layout_height="2.5dp"        android:layout_centerHorizontal="true"        android:background="@color/Theme_color"         />    <com.lixin.zhongshitongapp.app.view.LazyViewPager        android:id="@+id/ViewPager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout></span>

ViewPager代码

监听

 public class Pagechage implements LazyViewPager.OnPageChangeListener {        @Override        public void onPageScrollStateChanged(int arg0) {            // TODO Auto-generated method stub            if (arg0 == 2) {                int i = viewPager.getCurrentItem();                clean();//清除选中状态                count(i);//选中监听            }        }        @Override        public void onPageScrolled(int arg0, float arg1, int arg2) {            // TODO Auto-generated method stub            /**             * arg0 :当前页面,及你点击滑动的页面 arg1:当前页面偏移的百分比             * arg2:当前页面偏移的像素位置             */            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) id_tab_line_iv.getLayoutParams();            if (currentIndex == 0 && arg0 == 0)// 0->1            {                lp.leftMargin = (int) (arg1 * (screenWidth * 1.0 / 3) + currentIndex                        * (screenWidth / 3));            } else if (currentIndex == 1 && arg0 == 0) // 1->0            {                lp.leftMargin = (int) (-(1 - arg1)                        * (screenWidth * 1.0 / 3) + currentIndex                        * (screenWidth / 3));            } else if (currentIndex == 1 && arg0 == 1) // 1->2            {                lp.leftMargin = (int) (arg1 * (screenWidth * 1.0 / 3) + currentIndex                        * (screenWidth / 3));            } else if (currentIndex == 2 && arg0 == 1) // 2->1            {                lp.leftMargin = (int) (-(1 - arg1)                        * (screenWidth * 1.0 / 3) + currentIndex                        * (screenWidth / 3));            }            id_tab_line_iv.setLayoutParams(lp);        }        @Override        public void onPageSelected(int arg0) {            // TODO Auto-generated method stub            currentIndex = arg0;        }    }

在onCreate()方法加入

/**     * 设置滑动条的宽度为屏幕的1/3(根据Tab的个数而定)     */    private void initTabLineWidth() {        DisplayMetrics dpMetrics = new DisplayMetrics();        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);        screenWidth = dpMetrics.widthPixels;        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) id_tab_line_iv.getLayoutParams();        lp.width = screenWidth / 3;        id_tab_line_iv.setLayoutParams(lp);    }


 private class Click implements View.OnClickListener {        @Override        public void onClick(View arg0) {            // TODO Auto-generated method stub            clean();            count(arg0.getId());        }    }






1 0
原创粉丝点击