一:布局
界面布局:
<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">
<com.bwei.view.Shaby
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/main_shaby" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
自定义View布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="10dp"
android:text="呵呵"/>
<TextView
android:id="@+id/tv_line"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#ff0000"/>
</LinearLayout>
二:在MAinActivity中:
public class MainActivity extends Activity {
private String[] title = { "哈哈", "呵呵", "吼吼", "啦啦", "呀呀", "嘎嘎", "飞飞", "吱吱",
"唧唧", "歪歪", "哒哒", "QQ", "滴滴", "嘻嘻" };
public int[] pic = { R.drawable.aa, R.drawable.ab, R.drawable.ac,
R.drawable.ad, R.drawable.ae, R.drawable.af, R.drawable.ag,
R.drawable.al, R.drawable.am, R.drawable.an, R.drawable.ao,
R.drawable.ap, R.drawable.aq, R.drawable.av };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Shaby main_shaby = (Shaby) findViewById(R.id.main_shaby);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
main_shaby.initTitles(title);
main_shaby.setViewPager(viewPager);
viewPager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public int getCount() {
return pic.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(pic[position]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
});
}
}
三:自定义View中
public class Shaby extends HorizontalScrollView implements OnClickListener {
private LinearLayout linearLayout;
private String[] title;
private ViewPager viewPager;
public ArrayList<View> viewList=new ArrayList<View>();
public Shaby(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initData();
}
public Shaby(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Shaby(Context context) {
this(context, null);
}
private void initData() {
linearLayout = new LinearLayout(getContext());
this.addView(linearLayout);
}
public void setViewPager(ViewPager viewPager){
this.viewPager=viewPager;
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getCurrentItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public void initTitles(String[] title){
this.title=title;
viewList.clear();
for (int i = 0; i < title.length; i++) {
View view = View.inflate(getContext(), R.layout.title_item, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_line = (TextView) view.findViewById(R.id.tv_line);
viewList.add(view);
tv_title.setText(title[i]);
if(i==0){
tv_title.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
}else{
tv_title.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
view.setId(i);
view.setOnClickListener(this);
LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);
linearLayout.addView(view, params);
}
}
@Override
public void onClick(View v) {
getCurrentItem(v.getId());
}
private void getCurrentItem(int position) {
int width = linearLayout.getChildAt(position).getWidth();
this.scrollTo(width*position, 0);
for (int i = 0; i < title.length; i++) {
View view = viewList.get(i);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_line = (TextView) view.findViewById(R.id.tv_line);
if(position==i){
tv_title.setTextColor(Color.RED);
tv_line.setVisibility(View.VISIBLE);
viewPager.setCurrentItem(position);
}else{
tv_title.setTextColor(Color.BLACK);
tv_line.setVisibility(View.GONE);
}
}
}
}