在xml布局文件中:
ViewPager适配器可以自定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
-
<HorizontalScrollView
android:id="@+id/home_hsv"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:fadingEdge="none"
android:scrollbars="none" >
-
<RelativeLayout
android:id="@+id/rl_nav"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#5AB0EB" >
<RadioGroup
android:id="@+id/home_group"
android:layout_width="fill_parent"
android:layout_height="38dip"
android:layout_alignParentTop="true"
android:background="#F2F2F2"
android:orientation="horizontal" >
</RadioGroup>
<ImageView
android:id="@+id/home_img"
android:layout_width="1dip"
android:layout_height="5dip"
android:layout_alignParentBottom="true"
android:background="#0f0"
android:scaleType="matrix" />
</RelativeLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/home_vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在Fragment中
package com.bwie.mynews.fragment;
import java.util.zip.Inflater;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.bwie.mynews.R;
import com.bwie.mynews.adapter.HomeVpAdapter;
import com.bwie.mynews.base.BaseFragment;
import com.bwie.mynews.data.MyData;
import com.bwie.mynews.http.CustomerHttpClient;
import com.google.gson.Gson;
public class TypeFragment extends BaseFragment {
private View view;
private RadioGroup homeGroup;
private HorizontalScrollView homeHsv;
private ViewPager homeVp;
private ImageView homeImg;
private int indicatorWidth;
private LayoutInflater mInflater;
private int currentIndicatorLeft = 0;
@Override
public View getView(LayoutInflater inflater) {
view = inflater.inflate(R.layout.fragment_type, null);
return view;
}
@Override
public void findView() {
homeGroup = (RadioGroup) view.findViewById(R.id.home_group);
homeHsv = (HorizontalScrollView) view.findViewById(R.id.home_hsv);
homeVp = (ViewPager) view.findViewById(R.id.home_vp);
homeImg = (ImageView) view.findViewById(R.id.home_img);
}
@Override
public void initData() {
}
@Override
public boolean hasHttp() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setData() {
initImg();
initHsz();
initVp();
//设置缓存的最大数目
// homeVp.setOffscreenPageLimit(4);
}
@Override
public void setListener() {
homeVp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if(homeGroup!=null && homeGroup.getChildCount()>arg0){
((RadioButton)homeGroup.getChildAt(arg0)).performClick();
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
homeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(homeGroup.getChildAt(checkedId)!=null){
TranslateAnimation animation = new TranslateAnimation(
currentIndicatorLeft ,
((RadioButton) homeGroup.getChildAt(checkedId)).getLeft(), 0f, 0f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(100);
animation.setFillAfter(true);
//执行位移动画
homeImg.startAnimation(animation);
homeVp.setCurrentItem(checkedId); //ViewPager 跟随一起 切换
//记录当前 下标的距最左侧的 距离
currentIndicatorLeft = ((RadioButton) homeGroup.getChildAt(checkedId)).getLeft();
homeHsv.smoothScrollTo(
(checkedId > 1 ? ((RadioButton) homeGroup.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) homeGroup.getChildAt(2)).getLeft(), 0);
}
}
});
}
private void initHsz() {
//获取布局填充器
mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < MyData.HOME_TOP_ITEM.length; i++) {
RadioButton rb=(RadioButton) mInflater.inflate(R.layout.item_radiogroup,null);
rb.setId(i);
rb.setText(MyData.HOME_TOP_ITEM[i]);
rb.setLayoutParams(new LayoutParams(indicatorWidth,
LayoutParams.MATCH_PARENT));
homeGroup.addView(rb);
}
}
private void initImg() {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
indicatorWidth = dm.widthPixels / 4;
LayoutParams cursor_Params = homeImg.getLayoutParams();
cursor_Params.width = indicatorWidth;// 初始化滑动下标的宽
homeImg.setLayoutParams(cursor_Params);
}
private void initVp() {
HomeVpAdapter adapter=new HomeVpAdapter(this.getChildFragmentManager(), getActivity(), MyData.HOME_TOP_ITEM);
homeVp.setAdapter(adapter);
}
}
ViewPager适配器可以自定义