Android uses ViewPager to simulate the main interface-android learning Tour (78), viewpager-android
The first step is to introduce the ViewPager control, which requires pagerAdapter to provide data as a container. The data source of pagerAdapter is the View array as follows:
Some code is as follows:
mPagerAdapter = new PagerAdapter(){ @Override public int getCount() { return mViews.size(); } @Override public boolean isViewFromObject(View view, Object o) { return view == o; } @Override public Object instantiateItem(ViewGroup container, int position) { View view = mViews.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mViews.get(position)); } };
The View array contains several LinearLayout values. The Code is as follows:
private List<View> mViews = new ArrayList<View>(); View tab01 = inflater.inflate(R.layout.tab01,null); View tab02 = inflater.inflate(R.layout.tab02,null); View tab03 = inflater.inflate(R.layout.tab03,null); View tab04 = inflater.inflate(R.layout.tab04,null); mViews.add(tab01); mViews.add(tab02); mViews.add(tab03); mViews.add(tab04);
Sliding Click Event of ViewPager
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { int currentItem = viewPager.getCurrentItem(); resetImg(); switch(currentItem){ case 0: btnWeixin.setImageResource(R.drawable.tab_weixin_pressed); break; case 1: btnFriend.setImageResource(R.drawable.tab_find_frd_pressed); break; case 2: btnAddress.setImageResource(R.drawable.tab_address_pressed); break; case 3: btnSetting.setImageResource(R.drawable.tab_settings_pressed); break; default: break; } } @Override public void onPageScrollStateChanged(int i) { } });
Similar to the home page, the effect is to slide the pages of ViewPager, And the icons below are also highlighted as the corresponding interface, and then click the bottom, the corresponding page will jump to the bottom of the jump logic lies in the code, ViewPager for each page is numbered from 0
viewPager.setCurrentItem(1);
Layout main code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:background="@drawable/title_bar"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="#ffffff" android:textSize="20sp" android:layout_gravity="center" android:textStyle="bold"/></LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "horizontal" android: layout_width = "match_parent" android: layout_height = "55dp" android: gravity = "center" android: background = "@ drawable/bottom_bar"> <LinearLayout android: id = "@ + id/tab_weixin" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButton android: id = "@ + id/tab_weixin_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/tab_weixin_pressed" android: background = "#00000000" android: clickable = "false"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "" android: textColor = "# ffffffff"/> </LinearLayout> <LinearLayout android: id = "@ + id/tab_friend" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButton android: id = "@ + id/tab_friend_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/tab_find_frd_normal" android: background = "#00000000" android: clickable = "false"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "friend" android: textColor = "# ffffffff"/> </LinearLayout> <LinearLayout android: id = "@ + id/tab_address" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButton android: id = "@ + id/tab_address_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/tab_address_normal" android: background = "#00000000" android: clickable = "false"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Address Book" android: textColor = "# ffffffff"/> </LinearLayout> <LinearLayout android: id = "@ + id/tab_setting" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <ImageButton android: id = "@ + id/tab_setting_img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/tab_settings_normal" android: background = "#00000000" android: clickable = "false"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Settings" android: textColor = "# ffffffff"/> </LinearLayout>
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" > <include layout="@layout/top"/> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"> </android.support.v4.view.ViewPager> <include layout="@layout/bottom"/></LinearLayout>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!! Project Source Code address is https://github.com/fengsehng/WeixinTest
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.