<?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:background="@color/bgColor"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<com.alidao.glory.widget.MyHorizontalScrollView
android:id="@+id/HorizontalScrollView"
android:layout_width="match_parent"
android:scrollbars="none"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</com.alidao.glory.widget.MyHorizontalScrollView>
<ImageView
android:id="@+id/cursor1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:scaleType="fitCenter"
android:src="@drawable/scrollview_left"
android:visibility="gone" />
<ImageView
android:id="@+id/cursor2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:scaleType="fitCenter"
android:src="@drawable/scrollview_right" />
</RelativeLayout>
<TextView style="@style/divider_horizontal" />
<android.support.v4.view.ViewPager
android:id="@+id/listPager"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</LinearLayout>
其次:創建下方的Viewpager是由多個fragment組成
private void createViewPager(final ArrayList<NewsTabBean> list) {
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
tabLayout.removeAllViews();
tabList.clear();
// 創建Tab
for (int i = 0; i < list.size(); i++) {
fragments.add(NewsFragment.instance(list.get(i).id));
if (BaseActivity.getScreenWidth(context) < 720) {
MaxTab = 3;
} else {
MaxTab = 4;
}
if (list.size() <= MaxTab) {// 根據屏幕像素顯示多少個
tabWidth = BaseActivity.getScreenWidth(context) / list.size();
} else {
tabWidth = BaseActivity.getScreenWidth(context) / MaxTab;
}
LayoutParams paramss = new LayoutParams(tabWidth, -2);
TextView tv = new TextView(context);
tv.setLayoutParams(paramss);
tv.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) });
tv.setBackgroundResource(R.drawable.tab_selector);
tv.setClickable(true);
tv.setGravity(Gravity.CENTER);
tv.setPadding(10, 10, 10, 10);
tv.setSingleLine(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setText(list.get(i).name);
// if(BaseActivity.getScreenWidth(context)<720){
// tv.setTextSize(17);
// }else{
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
.getDimensionPixelSize(R.dimen.inter_content_font_size2));
// }
if (i == 0) {
tv.setTextColor(getResources().getColor(R.color.red));
} else
tv.setTextColor(Color.BLACK);
tv.setTag(i);
tabList.add(tv);
tabLayout.addView(tv);
if (i != list.size() - 1) {// 分隔線
TextView divider = new TextView(context);
LayoutParams params = new LayoutParams((int) 1, -1);
params.topMargin = 10;
params.bottomMargin = 10;
divider.setLayoutParams(params);
divider.setBackgroundResource(android.R.color.darker_gray);
tabLayout.addView(divider);
}
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
for (TextView textView : tabList) {
textView.setTextColor(Color.BLACK);
}
TextView tv = (TextView) arg0;
tv.setTextColor(getResources().getColor(R.color.red));
int position = (Integer) arg0.getTag();
listPager.setCurrentItem(position);
}
});
}
// 創建內容
adapter.setList(fragments);
findViewById(R.id.progressBarLayout).setVisibility(View.GONE);
}
這是viewpager的adapter
public class FragmentAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments;
public FragmentAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
}
public void setList(List<Fragment> fragments) {
this.fragments = (ArrayList<Fragment>) fragments;
notifyDataSetChanged();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
最後就是:聯動處理,當viewpager變化的時候,上面的scrollview如何移動
private void initData() {
adapter = new FragmentAdapter(getSupportFragmentManager());
listPager.setAdapter(adapter);
listPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
// 根據tab數量顯示
if (tabWidth * position >= tabWidth * (MaxTab - 1)
|| position < lastPosition)
horizontalScrollView.smoothScrollTo(tabWidth
* (position - (MaxTab - 2)), 0);
lastPosition = position;
for (int i = 0; i < tabList.size(); i++) {
TextView textView = tabList.get(i);
if (i == position) {
textView.setTextColor(getResources().getColor(
R.color.red));
} else {
textView.setTextColor(Color.BLACK);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
注:此分享代碼不完整,只爲思想共享,如果有問題歡迎私信交流。。。