标签:
为了美观,这里只显示关键代码,工程的整体代码可以到我的博客中下载:http://120.198.244.55:9999/files.cnblogs.com/files/ghylzwsb/scroll.rar
public Object instantiateItem(ViewGroup container, int position) { if (mViewList.get(position).getParent() != null) { container.removeView(mViewList.get(position)); } container.addView(mViewList.get(position)); return mViewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mViewList.get(position)); } @Override public int getCount() { return mViewList.size(); } }); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){ int x = (int)((position + positionOffset) * mTextView.getWidth()); ((View)mTextView.getParent()).scrollTo(-x, mTextView.getScrollY()); } public void onPageSelected(int position){ if(position == 0){ radioButton1.setChecked(true); }else if(position == 1){ radioButton2.setChecked(true); }else if(position == 2){ radioButton3.setChecked(true); }else if(position == 3){ radioButton4.setChecked(true); } } public void onPageScrollStateChanged(int state){ } }); } int tmpState = 0; public int dip2px(float dipValue) { final float scale = getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } public int px2dip(float pxValue) { final float scale = getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
<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:background="#FFFFFFFF" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/radioButton1" style="@style/radioButtonStyle" android:text="页面1" /> <RadioButton android:id="@+id/radioButton2" style="@style/radioButtonStyle" android:text="页面2" /> <RadioButton android:id="@+id/radioButton3" style="@style/radioButtonStyle" android:text="页面3" /> <RadioButton android:id="@+id/radioButton4" style="@style/radioButtonStyle" android:text="页面4" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="3dip" android:orientation="horizontal" > <TextView android:id="@+id/textView" android:layout_width="200dip" android:layout_height="3dip" android:background="#FF336699" android:textColor="#FFFF0000" /> </LinearLayout> <View android:layout_width="wrap_content" android:layout_height="1dip" android:background="#FF336699" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> </LinearLayout>
2016-04-10
BOB
标签:
原文地址:http://www.cnblogs.com/ghylzwsb/p/5374431.html