package com.bawei.hor;get import java.util.ArrayList; import java.util.List;it 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); } } } |