总结了之前用的Fragment+ViewPager,做的选项卡功能,今天做个Demo.
Demo下载:http://download.csdn.net/detail/hello_12413/8873827
效果如图所示:
主要代码如下所示:
package com.example.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.viewpagerdemo.fragment.AppListFragment;
import com.example.viewpagerdemo.fragment.BaseFragment;
import com.example.viewpagerdemo.fragment.LuckFragment;
import com.example.viewpagerdemo.fragment.MainFragment;
import com.example.viewpagerdemo.fragment.PersonFragment;
public class MainActivity extends FragmentActivity implements OnClickListener {
private ViewPager pager;
private List<Fragment> list;
private ImageView img1, img2, img3, img4;
private TextView header_title;
private ImageView imageView;
private int offset = 0;
private int currIndex = 0;
private int bmpW;
private BaseFragment mainFragment, appListFragment, personFragment, webFragment;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
void init() {
pager = (ViewPager) findViewById(R.id.vPager);
img1 = (ImageView) findViewById(R.id.img1);
img2 = (ImageView) findViewById(R.id.img2);
img3 = (ImageView) findViewById(R.id.img3);
img4 = (ImageView) findViewById(R.id.img4);
header_title = (TextView) findViewById(R.id.header_title);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
img3.setOnClickListener(this);
img4.setOnClickListener(this);
adapter = new MyAdapter(getSupportFragmentManager());
mainFragment = new MainFragment();
appListFragment = new AppListFragment();
personFragment = new PersonFragment();
webFragment = new LuckFragment();
InitImageView();
list = new ArrayList<Fragment>();
list.add(mainFragment);
list.add(appListFragment);
list.add(personFragment);
list.add(webFragment);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
changeTitle(0);
pager.setOnPageChangeListener(new MyOnPageChangeListener());
}
// 设置标题
private void changeTitle(int pagerId) {
switch (pagerId) {
case 0:
header_title.setText(R.string.index);
break;
case 1:
header_title.setText(R.string.list);
break;
case 2:
header_title.setText(R.string.center);
break;
case 3:
header_title.setText(R.string.luck);
break;
default:
break;
}
}
//初始化活动图片
private void InitImageView() {
imageView = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.below_img).getWidth();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (screenW / 4 - bmpW) / 2;
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
imageView.setImageMatrix(matrix);
}
// Viewpager Adapter
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
@Override
public int getCount() {
return list.size();
}
}
// ViewPager 变化监听
public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW;
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
//每次翻页,移动标记图片
public void onPageSelected(int arg0) {
Animation animation = new TranslateAnimation(one * currIndex, one * arg0, 0, 0);
currIndex = arg0;
changeTitle(arg0);
animation.setFillAfter(true);// True
animation.setDuration(300);
imageView.startAnimation(animation);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img1:
pager.setCurrentItem(0);
break;
case R.id.img2:
pager.setCurrentItem(1);
break;
case R.id.img3:
pager.setCurrentItem(2);
break;
case R.id.img4:
pager.setCurrentItem(3);
break;
default:
break;
}
}
}