ActionBar的tab栏上左右滑动的横线怎么像微信那样平滑的滑动
发布网友
发布时间:2022-04-23 21:37
我来回答
共1个回答
热心网友
时间:2023-10-10 13:53
package com.tematch.androidsample;
public class Activity2 extends Activity{
private ViewPager vpager;
public static final int MAX_TAB_SIZE = 3;
public static final String ARGUMENTS_NAME = "args";
public static final String ARG_PLANET_NUMBER = "planet_number";
private LayoutInflater inflater;
private ListView lv;
private ImageView iv;// 页卡标题动画图片
private LinearLayout linearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private int textViewW = 0;// 页卡标题的宽度
private int currIndex = 0;// 当前页卡编号
private List<View> listviews;
private View view1,view2,view3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
iv = (ImageView) findViewById(R.id.cursor);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
vpager = (ViewPager) this.findViewById(R.id.vpager);
vpager.setOffscreenPageLimit(2);
initview();
initViewPager();
InitTextView();
InitImageView();
}
/* 初始化页卡标题 */
private void InitTextView() {
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv1.setOnClickListener(new MyOnClickListener(0));
tv2.setOnClickListener(new MyOnClickListener(1));
tv3.setOnClickListener(new MyOnClickListener(2));
}
/* 标题点击监听 */
public class MyOnClickListener implements android.view.View.OnClickListener{
private int index = 0;
public MyOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
vpager.setCurrentItem(index);
}
}
public void initview(){
//监听页面改变的事件
vpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if (textViewW == 0) {
textViewW = tv1.getWidth();
}
Animation animation = new TranslateAnimation(textViewW * currIndex,
textViewW * arg0, 0, 0);
currIndex = arg0;
animation.setFillAfter(true);/* True:图片停在动画结束位置 */
animation.setDuration(300);
iv.startAnimation(animation);
setTextTitleSelectedColor(arg0);
setImageViewWidth(textViewW);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/* 设置标题文本的颜色 */
private void setTextTitleSelectedColor(int arg0) {
int count = vpager.getChildCount();
for (int i = 0; i < count; i++) {
TextView mTextView = (TextView) linearLayout.getChildAt(i);
if (arg0 == i) {
mTextView.setTextColor(0xffc80000);
} else {
mTextView.setTextColor(0xff969696);
}
}
}
/* 设置图片宽度 */
private void setImageViewWidth(int width) {
if (width != iv.getWidth()) {
LayoutParams laParams = (LayoutParams) iv.getLayoutParams();
laParams.width = width;
iv.setLayoutParams(laParams);
}
}
/* 初始化动画 */
private void InitImageView() {
Matrix matrix = new Matrix();
matrix.postTranslate(0, 0);
iv.setImageMatrix(matrix);// 设置动画初始位置
}
/* 初始化ViewPager */
private void initViewPager() {
listviews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
/**
* 布局一的界面
*/
view1 = mInflater.inflate(R.layout.fragment_main, null);
/**
* 布局二的界面
*/
view2 = mInflater.inflate(R.layout.fragment_weixin, null);
/**
* 布局三的界面
*/
view3 = mInflater.inflate(R.layout.fragment_weixin, null);
listviews.add(view1);
listviews.add(view2);
listviews.add(view3);
vpager.setAdapter(new MyPagerAdapter(listviews));
vpager.setCurrentItem(0);
vpager.setOnPageChangeListener(new MyOnPageChangeListener());
}
/**
* ViewPager适配器
*/
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;
public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
/* 页卡切换监听 */
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
if (textViewW == 0) {
textViewW = tv1.getWidth();
}
Animation animation = new TranslateAnimation(textViewW * currIndex,
textViewW * arg0, 0, 0);
currIndex = arg0;
animation.setFillAfter(true);/* True:图片停在动画结束位置 */
animation.setDuration(300);
iv.startAnimation(animation);
setTextTitleSelectedColor(arg0);
setImageViewWidth(textViewW);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
/**
* 返回上一界面
*/
public void back(View v){
finish();
}
}
}
xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
style="@android:style/Theme.Holo.Light"
>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="刷卡消费"
android:textColor="#969696"
android:textSize="17dp" />
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#969696"
android:text="现金消费"
android:textSize="17dp" />
<TextView
android:id="@+id/text3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="消费明细"
android:textColor="#969696"
android:textSize="17dp" />
</LinearLayout>
<ImageView
android:id="@+id/cursor"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/nav_bar_focus"
android:src="@drawable/nav_bar_focus" />
<android.support.v4.view.ViewPager
android:id="@+id/vpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是我最近写的,你看看是不是你要的效果