Viewpager+Fragment联动+水平滚动

在xml布局文件中:
<?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适配器可以自定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值