Android短信软件(1)ViewPager实现Tab

  ViwPager来自android.support.v4.view,此包主要是解决界面开发的兼容性问题,官网链接:http://developer.android.com/reference/android/support/v4/view/package-summary.html

主要代码:

Android短信软件(1)ViewPager实现Tab
public class SmsViewPagerActivity extends BaseActivity {

    private ViewPager viewPager;// 页卡内容
    private ImageView imageView;// 动画图片
    private List<View> views;// Tab页面列表
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度

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

        InitImageView();
        InitTextView();
        InitViewPager();

    }

    private void InitViewPager() {
        viewPager = (ViewPager) findViewById(R.id.vPager);
        views = new ArrayList<View>();
        LayoutInflater inflater = getLayoutInflater();
        View view1 = inflater.inflate(R.layout.show_messages_all, null);
        View view2 = inflater.inflate(R.layout.show_messages_collect, null);
        views.add(view1);
        views.add(view2);
        viewPager.setAdapter(new MyViewPagerAdapter(views));
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 初始化头标
     */
    private void InitTextView() {
        TextView textView1 = (TextView) findViewById(R.id.sms_show_message);
        TextView textView2 = (TextView) findViewById(R.id.sms_show_collect);

        textView1.setOnClickListener(new MyOnClickListener(0));
        textView2.setOnClickListener(new MyOnClickListener(1));

    }

    /**
     * 2 * 初始化动画
     */
    private void InitImageView() {
        imageView = (ImageView) findViewById(R.id.cursor);
        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.sms_sign).getWidth();// 获取图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        imageView.setImageMatrix(matrix);// 设置动画初始位置
    }

    /**
     * 
     * 头标点击监听 3
     */
    private class MyOnClickListener implements OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }

    }
    public class MyOnPageChangeListener implements OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TranslateAnimation移动特效,参数为始末两端点的坐标
            Animation animation = new TranslateAnimation(one * currIndex, one* arg0, 0, 0);
            currIndex = arg0;
            animation.setFillAfter(true);//图片停在动画结束位置
            animation.setDuration(300);
            imageView.startAnimation(animation);
        }

    }
}
Android短信软件(1)ViewPager实现Tab

 

适配器:

Android短信软件(1)ViewPager实现Tab
public class MyViewPagerAdapter extends PagerAdapter{
    private List<View> mListViews;
    
    public MyViewPagerAdapter(List<View> mListViews) {
        this.mListViews = mListViews;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)     {    
        container.removeView(mListViews.get(position));
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {            
         container.addView(mListViews.get(position), 0);
         return mListViews.get(position);
    }

    @Override
    public int getCount() {            
        return  mListViews.size();
    }
    
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {            
        return arg0==arg1;
    }
}
Android短信软件(1)ViewPager实现Tab

布局文件:

Android短信软件(1)ViewPager实现Tab
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/conversation_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/v5_title_bar_bg_light">

        <LinearLayout
            android:id="@+id/sms_head"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="4dp"
            android:layout_gravity="center">

            <TextView
                android:id="@+id/sms_show_message"
                style="@style/white_head_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/shortmessage" />

            <TextView
                android:id="@+id/sms_show_collect"
                style="@style/white_head_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/collectmessage" />
        </LinearLayout>

        <ImageView
            android:id="@+id/cursor"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sms_head"
            android:background="#00000000"
            android:scaleType="matrix"
            android:src="@drawable/sms_sign" />
    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>
Android短信软件(1)ViewPager实现Tab

效果图:

Android短信软件(1)ViewPager实现Tab

 参考博客:http://www.cnblogs.com/dwinter/archive/2012/02/27/2369590.html

Android短信软件(1)ViewPager实现Tab

上一篇:POJ 3686 The Windy's


下一篇:Windows中的DOS命令