/Hor/res/layout/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:orientation="vertical"> <HorizontalScrollView android:id="@+id/hor" android:layout_width="match_parent" android:layout_height="50dp" > <LinearLayout android:id="@+id/lin" android:layout_width="wrap_content" android:layout_height="wrap_content" ></LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/viewp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </android.support.v4.view.ViewPager> </LinearLayout>
/Hor/res/layout/f1.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" > <TextView android:id="@+id/t" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sdsss" /> </LinearLayout>
com.bawei.hor.MainActivity 主页
package com.bawei.hor; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends FragmentActivity { private String[] name=new String[]{"1","2","3","4","5","6","7"}; private List<Fragment> list; private List<TextView> t_list; private HorizontalScrollView horizontalScrollView; private int width; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hor); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lin); final ViewPager viewPager= (ViewPager) findViewById(R.id.viewp); //获取屏幕宽度 width = getWindowManager().getDefaultDisplay().getWidth(); //加载viewpager页面 steData(); //viewpager适配 FragmentManager fm = getSupportFragmentManager(); viewPager.setAdapter(new MyFragment(fm,list)); //viewpager监听 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub //判断滑到那页 getpage(arg0); } @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 } }); //最上面导航 t_list = new ArrayList<TextView>(); for(int i=0;i<name.length;i++){ //每个textview的宽度 LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width/3,LayoutParams.WRAP_CONTENT); TextView textView=new TextView(this); //距离上下左右 textView.setPadding(20, 20, 20, 20); //赋值 textView.setText(name[i]); //添加集合 t_list.add(textView); //绑定 textView.setTag(i); //将textview放到布局里 linearLayout.addView(textView, i,params); //textview 监听 点哪个滑到那页 textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int m = (Integer) v.getTag(); viewPager.setCurrentItem(m); } }); } } //viewpager监听里的 private void getpage(int arg0) { // TODO Auto-generated method stub //当前显示textview页 TextView textView1 = t_list.get(arg0); for (int i = 0; i < t_list.size(); i++) { //将当前显示的textview放到中间 TextView text=t_list.get(arg0); int k = text.getMeasuredWidth(); int l = text.getLeft(); int s = k / 2 + l - width / 2; horizontalScrollView.smoothScrollTo(s, 0); //得到所有textview TextView t = t_list.get(i); //给当前显示页面的textview页变色 if (!t.equals(textView1)) { t.setTextColor(Color.BLACK); } else { t.setTextColor(Color.RED); } } } //new page页 private void steData() { list = new ArrayList<Fragment>(); for(int i=0;i<name.length;i++){ //传值第几页 Fragment1 f1=new Fragment1(name[i]); list.add(f1); } } }
com.bawei.hor.MyFragment viewpager适配
package com.bawei.hor; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.view.View; public class MyFragment extends FragmentPagerAdapter { List<Fragment> list; public MyFragment(FragmentManager fm, List<Fragment> list) { super(fm); // TODO Auto-generated constructor stub this.list=list; } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } }
com.bawei.hor.Fragment1 fragment页
package com.bawei.hor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment1 extends Fragment{ String name; public Fragment1(String name) { // TODO Auto-generated constructor stub this.name=name; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view=inflater.inflate(R.layout.f1, null); TextView textView= (TextView) view.findViewById(R.id.t); textView.setText(name); return view; } }