public class MainActivity extends FragmentActivity {
private HorizontalScrollView hsv;//
private LinearLayout nav_ll;//
private ViewPager nav_vp;
private int itemWidth;//
private int selectItemIndex = 0;
private int screenWidth;
private List<String> type;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
type = MyTools.getType();
//
hsv = (HorizontalScrollView) findViewById(R.id.nav_HorizontalScrollView);
hsv.setHorizontalScrollBarEnabled(false);
nav_ll = (LinearLayout) findViewById(R.id.nav_ll);
nav_vp = (ViewPager) findViewById(R.id.nav_vp);
//
setNav();
//
setViewPager();
}
private void setViewPager() {
//
List<Fragment> fragmentList = new ArrayList<Fragment>();
for (int i = 0; i < MyTools.getType().size(); i++) {
MyFragment frame = new MyFragment(type.get(i));
//
fragmentList.add(frame);
}
//
MyFragmentAdapter fragmentAdapter = new MyFragmentAdapter(
getSupportFragmentManager(), fragmentList);
nav_vp.setAdapter(fragmentAdapter);
//
nav_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
nav_vp.setCurrentItem(position);
selectType(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setNav() {
//
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
//
itemWidth = screenWidth / type.size();
Log.i("---->", MyTools.getType().toString());
for (int i = 0; i < MyTools.getType().size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
itemWidth, RadioGroup.LayoutParams.WRAP_CONTENT);
//
params.leftMargin = 10;
params.rightMargin = 10;
//
TextView localTextView = new TextView(this);
//
localTextView.setText(MyTools.getType().get(i));
localTextView.setTextSize(16.0f);
localTextView.setBackgroundResource(R.drawable.nav_tv_bg);
localTextView.setPadding(5, 15, 5, 15);
localTextView.setGravity(Gravity.CENTER);
if (selectItemIndex == i) {
localTextView.setSelected(true);
}
//
localTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < nav_ll.getChildCount(); i++) {
TextView tv = (TextView) nav_ll.getChildAt(i);
if (tv == v) {
tv.setSelected(true);
nav_vp.setCurrentItem(i);
} else {
tv.setSelected(false);
}
}
}
});
nav_ll.addView(localTextView, i, params);
}
}
public void selectType(int tab_position) {
selectItemIndex = tab_position;
//
for (int i = 0; i < nav_ll.getChildCount(); i++) {
TextView tv0 = (TextView) nav_ll.getChildAt(tab_position);
int k = tv0.getMeasuredWidth();
int l = tv0.getLeft();
int i2 = l + k / 2 - screenWidth / 2;//
hsv.smoothScrollTo(i2, 0);
}
//
for (int i = 0; i < nav_ll.getChildCount(); i++) {
View view = nav_ll.getChildAt(i);
if (tab_position == i) {
view.setSelected(true);
} else {
view.setSelected(false);
}
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<HorizontalScrollView
android:id="@+id/nav_HorizontalScrollView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="9"
android:fillViewport="false">
<LinearLayout
android:id="@+id/nav_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/nav_vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>