Android常用开源项目(三十九)

Android开发之实现选项卡切换效果(ViewPager与Fragment组合)

1.先看下运行效果图吧

Android开发之实现选项卡切换效果(ViewPager与Fragment组合)

运行效果图

2.再看下代码结构图

Android开发之实现选项卡切换效果(ViewPager与Fragment组合)

代码结构图

2.因为本示例使用了动画的效果,所以引入了第三方的nineoldandroids-2.4.0.jar包,用户可以在网上下载到。

<1>先看下Activity的实现方式

public class MainActivity extends FragmentActivity {

private ArrayList<Fragment> fragments;

private ViewPager viewPager;

private TextView tab_game;

private TextView tab_app;

private int line_width;

private View line;

private IFLYFullScreenAd iv;

private boolean isBack;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tab_game = (TextView) findViewById(R.id.tab_game);

tab_app = (TextView) findViewById(R.id.tab_app);

line = findViewById(R.id.line);

//initData();

// 初始化TextView动画

ViewPropertyAnimator.animate(tab_app).scaleX(1.2f).setDuration(0);

ViewPropertyAnimator.animate(tab_app).scaleY(1.2f).setDuration(0);

fragments = new ArrayList<Fragment>();

fragments.add(new APPFragment());

fragments.add(new GameFragment());

line_width = getWindowManager().getDefaultDisplay().getWidth()

/ fragments.size();

line.getLayoutParams().width = line_width;

line.requestLayout();

viewPager = (ViewPager) findViewById(R.id.viewPager);

viewPager.setAdapter(new FragmentStatePagerAdapter(

getSupportFragmentManager()) {

@Override

public int getCount() {

return fragments.size();

}

@Override

public Fragment getItem(int arg0) {

return fragments.get(arg0);

}

});

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override

public void onPageSelected(int arg0) {

changeState(arg0);

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

float tagerX = arg0 * line_width + arg2 / fragments.size();

ViewPropertyAnimator.animate(line).translationX(tagerX)

.setDuration(0);

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

});

tab_game.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

viewPager.setCurrentItem(1);

//initData();

}

});

tab_app.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

viewPager.setCurrentItem(0);

}

});

}

/* 根据传入的值来改变状态 */

private void changeState(int arg0) {

if (arg0 == 0) {

tab_app.setTextColor(getResources().getColor(R.color.green));

tab_game.setTextColor(getResources().getColor(R.color.gray_white));

ViewPropertyAnimator.animate(tab_app).scaleX(1.2f).setDuration(200);

ViewPropertyAnimator.animate(tab_app).scaleY(1.2f).setDuration(200);

ViewPropertyAnimator.animate(tab_game).scaleX(1.0f)

.setDuration(200);

ViewPropertyAnimator.animate(tab_game).scaleY(1.0f)

.setDuration(200);

} else {

tab_game.setTextColor(getResources().getColor(R.color.green));

tab_app.setTextColor(getResources().getColor(R.color.gray_white));

ViewPropertyAnimator.animate(tab_app).scaleX(1.0f).setDuration(200);

ViewPropertyAnimator.animate(tab_app).scaleY(1.0f).setDuration(200);

ViewPropertyAnimator.animate(tab_game).scaleX(1.2f)

.setDuration(200);

ViewPropertyAnimator.animate(tab_game).scaleY(1.2f)

.setDuration(200);

}

}

private void initData() {

iv = IFLYFullScreenAd.createFullScreenAd(this,

"D32E34AE66DA0F27C83A94ED6E42A1B4");

iv.setAdSize(IFLYAdSize.FULLSCREEN);

iv.setParameter(AdKeys.SHOW_TIME_FULLSCREEN, "3000");

iv.setParameter(AdKeys.DOWNLOAD_ALERT, "true");

iv.loadAd(new IFLYAdListener() {

@Override

public void onAdReceive() {

iv.showAd();

}

@Override

public void onAdFailed(AdError arg0) {

Toast.makeText(

getApplicationContext(),

arg0.getErrorCode() + "****"

+ arg0.getErrorDescription(), 0).show();

isBack = true;

}

@Override

public void onAdClose() {

isBack = true;

}

@Override

public void onAdClick() {

}

});

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

if (isBack) {

return super.onKeyDown(keyCode, event);

} else {

return true;

}

}

return super.onKeyDown(keyCode, event);

}

}

<2>下面给出Activity的布局文件activity_main.xml

<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/base_bg"

android:orientation="vertical" >

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="55dp"

android:background="@drawable/base_titlebar_bg" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

<TextView android:layout_width="0dp"

android:layout_weight="1"

android:text="软件"

android:gravity="center"

android:textSize="15sp"

android:id="@+id/tab_app"

android:textColor="@color/green"

android:layout_height="match_parent"/>

<TextView android:layout_width="0dp"

android:layout_weight="1"

android:text="游戏"

android:id="@+id/tab_game"

android:gravity="center"

android:textSize="15sp"

android:textColor="@color/gray_white"

android:layout_height="match_parent"/>

</LinearLayout>

<View

android:layout_width="0dp"

android:layout_height="3dp"

android:id="@+id/line"

android:layout_alignParentBottom="true"

android:background="@color/green" />

</RelativeLayout>

<android.support.v4.view.ViewPager

android:id="@+id/viewPager"

android:layout_width="match_parent"

android:layout_height="match_parent" >

</android.support.v4.view.ViewPager>

</LinearLayout>

最后,由于本示例很简单,Fragment的具体实现就不在给出,用户可以自己根据项目需要自己定义。

展开阅读全文

没有更多推荐了,返回首页