private NoScrollViewPager viewPager;
private RadioGroup radioGroup;
private void initView() {
viewPager = (NoScrollViewPager) findViewById(R.id.view_pager);
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
controlViewPagerSpeed();
radioGroup = (RadioGroup) findViewById(R.id.main_radio);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio_button0:
currentIndex = 0;
viewPager.setCurrentItem(0);
break;
case R.id.radio_button1:
currentIndex = 1;
viewPager.setCurrentItem(1);
break;
}
}
});
}
class FragmentAdapter extends FragmentPagerAdapter {
public static final int TAB_COUNT = 2;
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int positon) {
switch (positon) {
case 0:
suchaFragment = new SuchaFragment();
return suchaFragment;
case 1:
mineFragment = new MineFragment();
return mineFragment;
}
return null;
}
@Override
public int getCount() {
return TAB_COUNT;
}
}
FixedSpeedScroller mScroller = null;
/**
* viewpager 切换速度
*/
private void controlViewPagerSpeed() {
try {
Field mField;
mField = ViewPager.class.getDeclaredField("mScroller");
mField.setAccessible(true);
mScroller = new FixedSpeedScroller(viewPager.getContext(), new AccelerateInterpolator());
mScroller.setmDuration(0);
mField.set(viewPager, mScroller);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//activity 被系统回收后 不保存Fragment 的状态
//super.onSaveInstanceState(outState, outPersistentState);
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xgs.financepay.activity.MainActivity">
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#F9f9f9"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.xgs.view.DrawableLeftCenterRadioButton
android:id="@+id/radio_button0"
可添加背景图片 和 点击风格
android:tag="radio_button0"
android:text="@string/sucha"
android:gravity="center_vertical"
android:checked="true"
android:layout_marginRight="80dp"
/>
<com.xgs.view.DrawableLeftCenterRadioButton
android:id="@+id/radio_button1"
android:tag="radio_button1"
android:gravity="center_vertical"
android:text="@string/mine" />
</RadioGroup>
<View
android:layout_above="@id/main_radio"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/black"
/>
<com.xgs.view.NoScrollViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</com.xgs.view.NoScrollViewPager>
</RelativeLayout>
自定义按钮
/**
* Created by Administrator on 2016/9/23.
*/
public class DrawableLeftCenterRadioButton extends RadioButton {
public DrawableLeftCenterRadioButton(Context context) {
super(context);
}
public DrawableLeftCenterRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawableLeftCenterRadioButton(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if(drawables != null){
Drawable drawableLeft = drawables[0];
if(drawableLeft != null){
float textWidth = getPaint().measureText(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableWidth = 0;
drawableWidth = drawableLeft.getIntrinsicWidth();
float bodyWidth = textWidth + drawableWidth + drawablePadding;
canvas.translate((getWidth() - bodyWidth) / 2, 0);
}
}
super.onDraw(canvas);
}
}
public class NoScrollViewPager extends ViewPager {
private boolean noScroll = true;
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollViewPager(Context context) {
super(context);
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
/* return false;//super.onTouchEvent(arg0); */
if (noScroll)
return false;
else
return super.onTouchEvent(arg0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (noScroll)
return false;
else
return super.onInterceptTouchEvent(arg0);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item);
}
}