Viewpager+Fragment联动+水平滚动

原创 2016年06月01日 11:31:49
在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适配器可以自定义

相关文章推荐

ViewPager+Fragment实现自动循环滚动

  • 2016年03月08日 10:29
  • 2.65MB
  • 下载

Android_ViewPager_实现多个图片水平滚动

1.示意图                       2.实现分析 (1).xml配置

Viewpager + Fragment +FragmentPagerAdapter实现定时循环滚动效果

ViewPager + Fragment的使用此处不写,玩

ViewPager+Fragment的使用(页卡式切换下方带滚动条)

ViewPager+Fragment的用法在android开发中比较常见,最近刚好要实现这个功能,写个笔记记录下来,以供参考。 1.activity_main.xml activity_main....

ViewPager + Fragment 实现标签的滚动效果

效果图 点击标签,指示线滑动到当前的标签下。 用到的技术: activity中添加 fragement, ViewPager, 自定义绘制view - Activity中动态添加碎片的步骤 ...

Viewpager+Fragment实现滚动效果

四个Layout界面 对应四个Fragment。 主要的就是如何隐藏Fragment跟如何点击并显示对应的Fragment。底部的Button点击 @Override public void...

实现滚动tab切换fragment + viewpager

1.首先是界面的布局     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_paren...

使用ViewPager+Fragment来实现带滚动条的多屏滑动-IndicatorFragmentActivity

转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17201587 介绍 在android应用中,多屏滑动是一种很常见的风格,博主...

标题栏与水平滑动界面:TabLayout、ViewPager、Fragment;;引导页:ViewPager+View

1、布局中添加TabLayout 控件 ,需要添加依赖;使用相关的属性,需要定义命名空间 compile 'com.android.support:design:25.0.1' ----在app...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Viewpager+Fragment联动+水平滚动
举报原因:
原因补充:

(最多只允许输入30个字)