左右滑动的Tab

来源:互联网 发布:剑3少林捏脸数据 编辑:程序博客网 时间:2024/05/17 01:26

[文件] activity_main.ml 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">
 
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="焦点推荐"
            android:textColor="#000"
            android:textSize="15dp"/>
 
        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="今日关注"
            android:textColor="#000"
            android:textSize="15dp"/>
 
        <TextView
            android:id="@+id/text3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="新闻资讯"
            android:textColor="#000"
            android:textSize="15dp"/>
 
        <TextView
            android:id="@+id/text4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="专题活动"
            android:textColor="#000"
            android:textSize="15dp"/>
    </LinearLayout>
 
    <ImageView
        android:id="@+id/cursor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/ic_xian_red"/>
 
    <android.support.v4.view.ViewPager
        android:id="@+id/tabpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>
 
</LinearLayout>

2. [文件] MainActivity.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
packagecom.example.demo;
 
importjava.util.ArrayList;
importjava.util.List;
 
importandroid.app.Activity;
importandroid.app.LocalActivityManager;
importandroid.content.Intent;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Color;
importandroid.graphics.Matrix;
importandroid.os.Bundle;
importandroid.support.v4.view.PagerAdapter;
importandroid.support.v4.view.ViewPager;
importandroid.support.v4.view.ViewPager.OnPageChangeListener;
importandroid.util.DisplayMetrics;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.view.animation.Animation;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.ImageView;
importandroid.widget.TextView;
 
publicclassMainActivity extendsActivity {
     
    privateViewPager mViewPager;// 页卡内容
    privateImageView mImageView;// 动画图片
    privateTextView tv1, tv2, tv3, tv4;
    privateintoffset = 0;// 动画图片偏移量
    privateintcurrIndex = 0;// 当前页卡编号
    privateintbmpWidth;// 动画图片宽度
    privateView view1, view2, view3, view4;// 各个页卡
    LocalActivityManager mLocalActivityManager = null;
 
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLocalActivityManager = newLocalActivityManager(this,true);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        initImageView();
        initTextView();
        initViewPager();
    }
 
    privatevoidinitViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.tabpager);
        List<View> views = newArrayList<View>();// Tab页面列表
        Intent intentA = newIntent(MainActivity.this, JDActivity.class);
        views.add(getView("A", intentA));
        Intent intentB = newIntent(MainActivity.this, JRActivity.class);
        views.add(getView("B", intentB));
        Intent intentC = newIntent(MainActivity.this, XWActivity.class);
        views.add(getView("C", intentC));
        Intent intentD = newIntent(MainActivity.this, ZTActivity.class);
        views.add(getView("D", intentD));
        mViewPager.setAdapter(newTabViewPagerAdapter(views));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(newMyOnPageChangeListener());
    }
 
    /**
     * 通过activity获取视图
     *
     */
    privateView getView(String id, Intent intent) {
        returnmLocalActivityManager.startActivity(id, intent).getDecorView();
    }
 
    /**
     * 初始化头标
     */
 
    privatevoidinitTextView() {
        tv1 = (TextView) findViewById(R.id.text1);
        tv2 = (TextView) findViewById(R.id.text2);
        tv3 = (TextView) findViewById(R.id.text3);
        tv4 = (TextView) findViewById(R.id.text4);
        tv1.setOnClickListener(newTabOnClickListener(0));
        tv2.setOnClickListener(newTabOnClickListener(1));
        tv3.setOnClickListener(newTabOnClickListener(2));
        tv4.setOnClickListener(newTabOnClickListener(3));
    }
 
    /**
     * 初始化动画
     */
 
    privatevoidinitImageView() {
        mImageView = (ImageView) findViewById(R.id.cursor);
        bmpWidth = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_xian_red).getWidth();// 获取图片宽度
        DisplayMetrics dm = newDisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        intscreenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 4- bmpWidth) / 2;// 计算偏移量
        Matrix matrix = newMatrix();
        matrix.postTranslate(offset,0);
        mImageView.setImageMatrix(matrix);// 设置动画初始位置
    }
 
    /**
     *
     * 头标点击监听
     */
    privateclassTabOnClickListener implementsOnClickListener {
        privateintindex = 0;
 
        publicTabOnClickListener(inti) {
            index = i;
        }
 
        publicvoidonClick(View v) {
            mViewPager.setCurrentItem(index);
        }
 
    }
 
    publicclassTabViewPagerAdapter extendsPagerAdapter {
        privateList<View> mListViews;
 
        publicTabViewPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }
 
        @Override
        publicvoiddestroyItem(ViewGroup container, intposition, Object object) {
            container.removeView(mListViews.get(position));
        }
 
        @Override
        publicObject instantiateItem(ViewGroup container, intposition) {
            container.addView(mListViews.get(position),0);
            returnmListViews.get(position);
        }
 
        @Override
        publicintgetCount() {
            returnmListViews.size();
        }
 
        @Override
        publicbooleanisViewFromObject(View arg0, Object arg1) {
            returnarg0 == arg1;
        }
    }
 
    publicclassMyOnPageChangeListener implementsOnPageChangeListener {
 
        intone = offset * 2+ bmpWidth;// 页卡1 -> 页卡2 偏移量
        inttwo = one * 2;// 页卡1 -> 页卡3 偏移量
 
        publicvoidonPageScrollStateChanged(intarg0) {
 
        }
 
        publicvoidonPageScrolled(intarg0,floatarg1,intarg2) {
 
        }
 
        publicvoidonPageSelected(intarg0) {
            Animation animation = null;
            switch(arg0) {
            case0:
                Log.d("TAG","0");
                tv1.setTextColor(Color.RED);
                tv2.setTextColor(Color.BLACK);
                tv3.setTextColor(Color.BLACK);
                tv4.setTextColor(Color.BLACK);
                break;
            case1:
                Log.d("TAG","1");
                tv1.setTextColor(Color.BLACK);
                tv2.setTextColor(Color.RED);
                tv3.setTextColor(Color.BLACK);
                tv4.setTextColor(Color.BLACK);
                break;
            case2:
                Log.d("TAG","2");
                tv1.setTextColor(Color.BLACK);
                tv2.setTextColor(Color.BLACK);
                tv3.setTextColor(Color.RED);
                tv4.setTextColor(Color.BLACK);
                break;
            case3:
                Log.d("TAG","3");
                tv1.setTextColor(Color.BLACK);
                tv2.setTextColor(Color.BLACK);
                tv3.setTextColor(Color.BLACK);
                tv4.setTextColor(Color.RED);
                break;
            }
            animation = newTranslateAnimation(one * currIndex, one * arg0, 0,
                    0);
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(100);
            mImageView.startAnimation(animation);
            Log.d("ReDianActivity:","您选择了"+ mViewPager.getCurrentItem()
                    +"页卡");
        }
    }
}

0 0
原创粉丝点击